Published Feb 12, 2019 / 2 min read

Understanding dependency resolutions in PHP

An attempt to understand Laravel's implementation of the automated dependency resolution. By replicating this functionality based on the tutorial from Dayle Rees.

Features of the IoC container

We are going to build the features of the dependency resolution IoC container step by step:

A. Build a container that stores and retrieves services

  1. build a container class
  2. add an instance() function that stores services
  3. add a make() function that retrieves services

B. Add inversion of control functionality

  1. add a bind() function that accepts closures to bind services
  2. update the make() function that it retrieves instances and bindings
  3. cache services as an instances once requested (singelton-concept)

C. Add automated (recursive) dependency resolution

  1. update the make() function with a class_exists() functionality
  2. refactor bind() function by extracting the dependency resolution into a autoResolve() function
  3. add recursive dependency resolution functionality

Concepts

This tutorial makes use of the following concepts.

1. Inversion of control

Services are created when they are needed. In contrast to when they are registered.

2. Dependency resolution

It is about automated creation of classes during the run time of an application.

3. PHP reflection classes

PHP has a reflection API that enable us to reverse engineer a class and retrieve information about the nature of the class and associated interfaces, functions, extensions.

To automatically resolve classes wit construction parameters we need to make use of the reflection API to receive the dependent class instantiate them accordingly.

Testing

You will find withing the tests directory a containerTest.php with 6 different test cases. Run all tests with -> ./vendor/bin/phpunit .