Tom Butler is a Web Developer, Ph.D student researching software best practices, and part-time University Lecturer from the UK with an interest in programming best practices, separation of concerns and a 'less is more' approach to code.

Tom's articles

  1. PHP Dependency Injection Container Performance Benchmarks

    Most frameworks and larger PHP applications utilize a Dependency Injection Container with the goal of a more maintainable codebase. However, this can have an impact on performance. As loading times matter, keeping sites fast is important as ever. Today I’m going to benchmark several PHP Dependency Injection containers to see what their relative performance is like.

    For those unfamiliar with the concept, a Dependency Injection Container is a piece of software which automatically builds an object tree. For example, consider a User object which requires a Database instance.

    $user = new User(new Database());
    

    A Dependency Injection Container can be used to automatically construct the object tree without needing to provide the parameters manually:

    $user = $container->get('User');
    

    Each time this is called, a user object will be created with the database object “injected”.

    There are several well known (and not so well known) containers available for PHP:

    • PHP-DI, a popular DI Container
    • Symfony\DependencyInjection, the the Dependency Injection Container provided by the Symfony framework
    • Zend\Di the Dependency Injection Container provided by Zend Framework
    • Orno\Di, a lesser known container with limited features but developed with performance in mind
    • Dice, another lesser known container with a focus on being lightweight
    • Aura.Di, a fairly popular container with minimal features

    A word on Pimple: Although Pimple is advertised as a Dependency Injection Container, retrieving an object from the container always returns the same instance, which makes Pimple a Service Locator rather than a Dependency Injection Container and as such, cannot be tested.

    Although all the containers support different features, this benchmark will cover the basic functionality required by a Dependency Injection Container. That is, creating objects and injecting dependencies where they’re needed.