entire-studio / dependency-injection
PSR-11 compatible dependency injection container
v1.2.0
2024-12-26 22:54 UTC
Requires
- php: >=8.2
- psr/container: ^2.0
Requires (Dev)
- ext-xdebug: *
- dealerdirect/phpcodesniffer-composer-installer: ^1
- phpcompatibility/php-compatibility: ^9
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11
- squizlabs/php_codesniffer: ^3
This package is auto-updated.
Last update: 2025-01-14 23:57:56 UTC
README
PSR-11 compatible dependency injection container
Installation
Install the latest version with
$ composer require entire-studio/dependency-injection
Basic Usage
<?php declare(strict_types=1); require_once __DIR__ . '/../vendor/autoload.php'; use EntireStudio\DependencyInjection\Container; // Create a DI container $di = new Container(); // Two concrete implementations with a common interface interface Wall {} class WoodenWall implements Wall {} class ConcreteWall implements Wall {} // A class with two properties, one of which is concrete and the other one is an interface readonly class House { public function __construct( private Wall $mainWall, private ConcreteWall $otherWalls, ) {} public function getWallClass(): string { return sprintf( '%s: %s' . PHP_EOL . '%s: %s' . PHP_EOL, 'Main wall', get_class($this->mainWall), 'Other walls', get_class($this->otherWalls), ); } } // Map the Wall interface to WoodenWall $di->set(Wall::class, WoodenWall::class); // Without this we wouldn't know whether to use Wooden or Concrete wall in place of the Wall interface // $di->set(House::class, House::class); // No need to register House as with interface mapped - we can reflect other params // Get the instance $house = $di->get(House::class); // Call the class method echo $house->getWallClass(); /* * Main wall: WoodenWall * Other walls: ConcreteWall */
$ php examples/basic.php
Other examples
See examples/
for more examples.
Commands
Development
composer test
- runs test suite.composer sat
- runs static analysis.composer style
- checks codebase against PSR-12 coding style.composer style:fix
- fixes basic coding style issues.