PHP dependency injection

by Tristan Claridge

Object-oriented (OO) programming involves dependencies between objects, such as objects that require a connection to a database object. It’s inevitable and required. The only other option would be duplicate code in every object, defeating the purpose of OO programming.

The amount of dependency is the degree that one component relies on another to perform its responsibilities. High dependency limits code reuse and makes moving components to new projects difficult. Lower dependency is better.

The key question then is how to manage the dependencies. Some people suggest they should be ‘injected’ into each object.

Dependency Injection (DI) is the solution to the principle of Inversion of Control (IoC). It manages the dependencies between objects by creating the dependent objects outside the object which uses that dependency. In other words instead of objects configuring themselves they are configured by an external entity.

In software engineering, IoC is an object-oriented programming practice where the object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.

Popular forms of DI are constructor injection, setter injection and IoC container.

We have created a framework that doesn’t require injection, but manages the dependencies from within object, but without having to wire every other dependent object independently in each object.

Constructor set dependencies

Dependencies are set in the constructor of the object, and then read and instantiated by a factory that wires all dependencies making them available from within the object.

The factory identifies the parameters in the constructor using:

// get the parameter names on a method’s signature
protected function _getMethodParameterNames($className, $methodName)
$parameterNames = array();
$methodDetails = new ReflectionMethod($className, $methodName);

foreach ($methodDetails->getParameters() as $parameter)
$parameterNames[] = $parameter->name;
return $parameterNames;

Dependencies are set in the object. For example:

function __construct($display, $db)
$this->m_display = $display;
$this->db = $db;

Within the object dependencies can be called as:

$this->m_display->addTemplate(‘login’); //add HTML to display

The benefits of this approach are that the dependencies are managed within each object and the factory can handle any missing or broken dependencies appropriately.

It also means that all dependent objects are wired, but no other unnecessary objects, reducing load. Complex executions may require a dozen objects through cascading dependencies. Other executions may require just one or two objects to be wired.

Our PHP framework follows an MVC pattern and manages dependencies from within each object making it perfect for large software development projects. It is flexible, efficient, and highly scalable.


Decompression is a web and software development studio. Our main focus is on intelligent and innovative projects. Our small team of developers are highly skilled and love programming. We also have a full time graphic designer who handles the aesthetics.

Decompression has been a registered company since 2004.

P: +61 (0) 7 5641 2550
E: tristan@decompression.com.au