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

This package is auto-updated.

Last update: 2025-05-27 22:46:22 UTC


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;