midnight / automatic-di
automatic dependency injection for PHP
Installs: 21 698
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 1
Requires
- php: ^7.4 || ^8.0
- psr/container: ^1.1 || ^2.0
Requires (Dev)
- eventjet/coding-standard: ^3.6
- infection/infection: ^0.21.4
- maglnet/composer-require-checker: ^3.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^0.12.83
- phpstan/phpstan-phpunit: ^0.12.16
- phpstan/phpstan-strict-rules: ^0.12.2
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-10-29 05:22:16 UTC
README
This package provides automatic dependency injection for PHP. It requires a container-interop-compatible container.
Installation
Composer: midnight/automatic-di
Usage
Let's say we have the following interfaces and classes:
interface FooInterface { } class Foo { private $bar; public function __construct(Bar $bar) { $this->bar = $bar; } } class Bar { } interface BazInterface { } class Baz { } class Hodor { private $baz; public function __construct(BazInterface $baz) { $this->baz = $baz; } }
Now we can use the AutomaticDiContainer
like this:
$wrappedContainer = new Some\Other\Container; // An implementation of Interop\Container\ContainerInterface $config = Midnight\AutomaticDi\AutomaticDiConfig::fromArray([ 'preferences' => [ FooInterface::class => Foo::class, ], 'classes' => [ Hodor::class => [ 'baz' => Baz::class, ], ], ]); $container = new Midnight\AutomaticDi\AutomaticDiContainer($wrappedContainer, $config); $foo = $container->get(FooInterface::class); // Returns an instance of Foo $hodor = $container->get(Hodor::class); // Returns an instance of Hodor with an injected Baz
Performance
This package works via reflection. Isn't that slow?
Yes, it is. A cache is coming soon.