Structural/DependencyInjection/README.rst
Dependency Injection__To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code.
DatabaseConfiguration gets injected and DatabaseConnection will get all that it
needs from $config. Without DI, the configuration would be created
directly in DatabaseConnection, which is not very good for testing and
extending it.
Connection object. For testing purposes,
one can easily create a mock object of the configuration and inject
that into the Connection object.. image:: uml/uml.png :alt: Alt DependencyInjection UML Diagram :align: center
You can also find this code on GitHub_
DatabaseConfiguration.php
.. literalinclude:: DatabaseConfiguration.php :language: php :linenos:
DatabaseConnection.php
.. literalinclude:: DatabaseConnection.php :language: php :linenos:
Tests/DependencyInjectionTest.php
.. literalinclude:: Tests/DependencyInjectionTest.php :language: php :linenos:
.. _GitHub: https://github.com/DesignPatternsPHP/DesignPatternsPHP/tree/main/Structural/DependencyInjection
.. __: http://en.wikipedia.org/wiki/Dependency_injection