Slick package for dependency injection container.

v2.8.0 2023-04-15 23:52 UTC


Slick Di package

Latest Version Software License Build Status Quality Score Total Downloads

slick/di is an easy dependency injection container for PHP 8.0+.

It aims to be very lightweight and tries to remove a lot of the guessing and magic stuff that dependency containers use those days.

It also allows you to nest containers witch can become very useful if you have several packages that you reuse in your applications, allowing you to define containers with default dependencies in those packages for later override and usage them in your application.

This package is compliant with PSR-2 code standards and PSR-4 autoload standards. It also applies the semantic version 2.0.0 specification.


Via Composer

$ composer require slick/di


To create a dependency container we need to create at least a services.php file with all our dependency definitions:

use Slick\Di\Container;
use Slick\Di\Definition\ObjectDefinition;

 * Dependency injection object definition example
return [
    'config' => [
        'color' => 'blue',
        'gear' => 'manual'
    'engineService' => ObjectDefinition::create(Engine::class)
    CarSettings::class => function(Container $container) {
        return new CarSettings($container->get('config'));

Now to build the dependency container we need to use the ContainerBuilder factory class like this:

use Slick\Di\ContainerBuilder;

$definitionsFile = __DIR__ . '/services.php';
$sources = dirname(__DIR__, 2). '/src';
$container = (new ContainerBuilder($definitionsFile))

With that, we are ready to create and inject dependencies with our container:

class Car
     * @var EngineInterface
    protected $engine;
    public function __construct(
        #[UserService(id: CarSettings::class)]
        CarSettings $settings
    ) {
        // $settings will be injected if created with the Container::make() method.

     * @inject engineService
     * @return self
    #[CallWith(service: "engineService")]
    public function setEngine(EngineInterface $engine)
        $this->engine = $engine;
        return $this;

$myCar = $container->make(Car::class);

Please refer to the full documentation site for more on slick/di package.


We use Behat to describe features and for acceptance tests and PHPSpec for unit testing.

# unit tests
$ vendor/bin/phpspec

# acceptance tests
$ vendor/bin/behat


Please see CONTRIBUTING for details.


If you discover any security related issues, please email instead of using the issue tracker.



The MIT License (MIT). Please see License File for more information.