angelobono / php-di-service-attribute
A PHP service attribute that automatically configures factories for (psr) di containers.
1.0.6
2025-05-27 22:46 UTC
Requires
- psr/container: ^1.0
Requires (Dev)
- php-di/php-di: ^7.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^12.1
- squizlabs/php_codesniffer: ^3.0
README
The factories based on constructor reflection, so everything will be processed at compile time only.
Install
composer require angelobono/php-di-service-attribute
Usage
If you have a class you can add the #[Service]
attribute to it:
#[Service] class TestService1 { public function __construct(TestService2 $service2) { // ... } } #[Service] class TestService2 { }
Then you can use a Container to automatically get an instance of the service:
$container = new Container(); Service::setContainer($container); $serviceInstance = $container->get(TestService1::class); $serviceInstance instanceof TestService1 === true;