mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-05 14:37:27 +02:00
PHP7 Dependency Injection
This commit is contained in:
@@ -10,17 +10,10 @@ testable, maintainable and extendable code.
|
||||
Usage
|
||||
-----
|
||||
|
||||
Configuration gets injected and ``Connection`` will get all that it
|
||||
DatabaseConfiguration gets injected and ``DatabaseConnection`` will get all that it
|
||||
needs from ``$config``. Without DI, the configuration would be created
|
||||
directly in ``Connection``, which is not very good for testing and
|
||||
extending ``Connection``.
|
||||
|
||||
Notice we are following Inversion of control principle in ``Connection``
|
||||
by asking ``$config`` to implement ``Parameters`` interface. This
|
||||
decouples our components. We don't care where the source of information
|
||||
comes from, we only care that ``$config`` has certain methods to
|
||||
retrieve that information. Read more about Inversion of control
|
||||
`here <http://en.wikipedia.org/wiki/Inversion_of_control>`__.
|
||||
directly in ``DatabaseConnection``, which is not very good for testing and
|
||||
extending it.
|
||||
|
||||
Examples
|
||||
--------
|
||||
@@ -45,27 +38,15 @@ Code
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
AbstractConfig.php
|
||||
DatabaseConfiguration.php
|
||||
|
||||
.. literalinclude:: AbstractConfig.php
|
||||
.. literalinclude:: DatabaseConfiguration.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Parameters.php
|
||||
DatabaseConnection.php
|
||||
|
||||
.. literalinclude:: Parameters.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
ArrayConfig.php
|
||||
|
||||
.. literalinclude:: ArrayConfig.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Connection.php
|
||||
|
||||
.. literalinclude:: Connection.php
|
||||
.. literalinclude:: DatabaseConnection.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
@@ -78,11 +59,5 @@ Tests/DependencyInjectionTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Tests/config.php
|
||||
|
||||
.. literalinclude:: Tests/config.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Structural/DependencyInjection
|
||||
.. __: http://en.wikipedia.org/wiki/Dependency_injection
|
||||
|
Reference in New Issue
Block a user