Find file History
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
Tests Replaced with strict assertions Sep 29, 2018
uml PHP7 Dependency Injection Sep 23, 2016
DatabaseConfiguration.php PHP7 Dependency Injection Sep 23, 2016
DatabaseConnection.php PHP7 Dependency Injection Sep 23, 2016
README.rst Add markdown flavors for objects. Dec 11, 2017

README.rst

Dependency Injection

Purpose

To implement a loosely coupled architecture in order to get better testable, maintainable and extendable code.

Usage

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.

Examples

  • The Doctrine2 ORM uses dependency injection e.g. for configuration that is injected into a Connection object. For testing purposes, one can easily create a mock object of the configuration and inject that into the Connection object
  • Symfony and Zend Framework 2 already have containers for DI that create objects via a configuration array and inject them where needed (i.e. in Controllers)

UML Diagram

Alt DependencyInjection UML Diagram

Code

You can also find this code on GitHub

DatabaseConfiguration.php

.. literalinclude:: DatabaseConfiguration.php
   :language: php
   :linenos:

DatabaseConnection.php

.. literalinclude:: DatabaseConnection.php
   :language: php
   :linenos:

Test

Tests/DependencyInjectionTest.php

.. literalinclude:: Tests/DependencyInjectionTest.php
   :language: php
   :linenos: