Use attributes for declarative dependency configuration.
Override type hints to inject a specific implementation:
use Gacela\Container\Attribute\Inject;
class NotificationService {
public function __construct(
#[Inject(EmailLogger::class)]
private LoggerInterface $logger,
) {}
}
// EmailLogger will be injected even if LoggerInterface is bound to FileLogger
$service = $container->get(NotificationService::class);#[Inject] also works on a property, for classes whose constructor is not yours
to change:
final class ReportBuilder extends FrameworkBaseClass
{
#[Inject]
private LoggerInterface $logger;
#[Inject(RedisCache::class)]
private CacheInterface $cache;
}Private, protected and inherited properties are all supported, including private properties declared on a parent class. Static properties are ignored. A promoted constructor parameter is injected by the constructor, never twice.
Constructor injection remains the default, and this is not an equal
alternative. Dependencies stop being visible in the signature, the property
cannot be readonly, and the object is briefly in an invalid state after new.
Reach for it when the constructor is out of your hands — a framework base class,
or legacy code — not as a matter of taste.
Before using it, check whether afterResolving() already covers
the case. It runs after resolution with the instance and the container in hand,
and needs no new machinery.
The most common reason people reach for property injection is to break a circular dependency. That does not work here, deliberately:
final class A { #[Inject] public B $b; }
final class B { #[Inject] public A $a; }
$container->get(A::class); // CircularDependencyExceptionProperty injection runs inside the same resolution stack as constructor
injection, so a cycle reached through a property is reported exactly like any
other. CircularDependencyException and its resolution chain are a feature of
this library, and this attribute does not open a hole in them.
| Property | Result |
|---|---|
readonly |
DependencyInvalidArgumentException — only the declaring class may write it |
| Untyped | DependencyInvalidArgumentException — nothing to resolve |
Scalar type (string, int, …) |
DependencyInvalidArgumentException — pass it through the constructor |
Nothing at all if you do not use it: classes with no #[Inject] property never
enter the injection path, and the reflection scan is memoized per class for the
lifetime of the process rather than per container.
When you do use it, expect roughly +15% on that class's resolution against
the constructor equivalent (1.099μs vs 0.959μs), which is the cost of
ReflectionProperty::setValue().
A class with injected properties is skipped by
writeCompiledFactories() — a generated new expression
cannot assign them — and resolves through the normal path instead.
#[Inject] on a public method has the container call it after construction:
final class ReportBuilder extends FrameworkBaseClass
{
private Clock $clock;
#[Inject]
public function setClock(Clock $clock): void
{
$this->clock = $clock;
}
}Arguments resolve through the same path a constructor's parameters do, so
contextual bindings, defaults and a nested #[Inject] naming an implementation
all behave identically:
#[Inject]
public function setRepository(
#[Inject(DatabaseRepository::class)] RepositoryInterface $repository,
): void { ... }Inherited methods are called. A #[Inject] marker on an interface method or a non-private parent method also applies to its implementation or override, without repeating the attribute. Each method runs once, using the implementation's parameter names, attributes and defaults. The constructor is never treated as an injected method, whatever it is annotated with.
Property injection covers "the constructor is not mine to change" only for fields you can write to, and it writes the field directly. A method can do two things it cannot:
- run validation or derive state, since it is a method body and not an assignment;
- be declared on an interface — a
ClockAwareInterfacewithsetClock()is a shape a property cannot express at all.
It is also the honest answer for two collaborators that need each other after construction but not during it. That is not a cycle in the resolution sense and a constructor cannot express it.
Fixed, because it is observable and someone will depend on it:
- the constructor
#[Inject]properties#[Inject]methods, in declaration order
So a setter can read anything the constructor or the properties set.
Refused by name, rather than skipped — an annotation that is silently ignored means a dependency that never arrived and nothing anywhere saying so:
| Method | Result |
|---|---|
static |
DependencyInvalidArgumentException — injection calls it on an instance |
private / protected |
DependencyInvalidArgumentException — the container calls from outside |
| Scalar or untyped parameter | DependencyInvalidArgumentException, exactly as on a constructor |
Nothing if you do not use it: the scan is memoized per class for the life of the
process, and a class with no #[Inject] method never enters the call path.
Under #[Lazy] the calls sit inside the initializer with the
constructor, so a lazy service still defers them until first touch. A class with
injected methods is skipped by writeCompiledFactories() —
a new expression cannot make the calls — with the reason InjectedMethod.
The four attributes are not final, and every read the container makes
passes ReflectionAttribute::IS_INSTANCEOF. So a package or framework wrapping
this container can offer them under its own name without its users importing a
vendor namespace:
namespace App\Attribute;
use Attribute;
#[Attribute(Attribute::TARGET_PARAMETER | Attribute::TARGET_PROPERTY | Attribute::TARGET_METHOD)]
final class Inject extends \Gacela\Container\Attribute\Inject
{
}#[App\Attribute\Inject] is then honoured exactly as #[Gacela\Container\Attribute\Inject]
is, including the optional implementation argument. The same works for
#[Singleton], #[Factory] and #[Lazy].
Repeat the #[Attribute(...)] declaration on the subclass with the targets you
want — PHP does not inherit it — and keep the targets within the parent's.
An exact-FQN match, which is what this used to do, follows neither a
subclass nor a class_alias(). That failure is silent: the parameter is simply
not injected and nothing says why.
Mark a class to be instantiated only once:
use Gacela\Container\Attribute\Singleton;
#[Singleton]
class DatabaseConnection {
public function __construct(private string $dsn) {}
}
$conn1 = $container->get(DatabaseConnection::class);
$conn2 = $container->get(DatabaseConnection::class);
// $conn1 === $conn2 (same instance)The same behaviour is available at runtime, without an attribute, via
singleton().
Always create fresh instances:
use Gacela\Container\Attribute\Factory;
#[Factory]
class RequestContext {
public function __construct(private LoggerInterface $logger) {}
}
$ctx1 = $container->get(RequestContext::class);
$ctx2 = $container->get(RequestContext::class);
// $ctx1 !== $ctx2 (different instances)Note: Attribute checks are cached internally, so repeated instantiations of the same class avoid repeated reflection.
Defers construction until the instance is first used.
use Gacela\Container\Attribute\Lazy;
#[Lazy]
final class ReportGenerator
{
public function __construct(private Database $db) {}
}
$report = $container->get(ReportGenerator::class);
// Nothing built yet — not the generator, not the Database behind it.
$report->run(); // constructed now, on first property accessWhat you get back is a real instance of the class, not a proxy subclass:
$report::class is ReportGenerator and instanceof behaves normally. There is
no generated proxy class and no extra dependency — this uses PHP's native lazy
objects.
A class with no instance properties, its own or inherited, cannot be lazy: PHP
treats a lazy object with nothing to defer as initialized at once and would skip
the constructor. The container builds such a class straight away instead, and a
lazy() factory for it is called straight away.
Internal PHP classes and their descendants, except stdClass, also build eagerly. A lazy() factory for such a class runs immediately and returns its result unchanged. stdClass descendants remain eligible for native deferral. See PHP's lazy-object restrictions.
A class with instance properties does not need a constructor to be lazy. Its
property defaults are preserved, and #[Inject] properties and methods are
initialized on first touch.
Dependencies are resolved inside the initializer, so a lazy service that is never touched costs nothing to build, and neither does its subtree.
That holds wherever the service appears, not just at the top of a get() call: a
lazy class injected into another class's constructor is handed over uninitialized,
so the holder can be built without paying for it.
Property access. A method that reads no properties never needs the constructor, so it will not trigger one:
$service->staticGreeting(); // returns a constant: still uninitialized
$service->db; // touches state: constructs nowlazy() does the same thing from the outside, for classes you cannot annotate
and for cases where laziness belongs to the app rather than the class:
// A vendor class, made lazy without touching it
$container->lazy(ReportGenerator::class);
// An abstract, lazily bound to a concrete
$container->lazy(ReportGeneratorInterface::class, PdfReportGenerator::class);
// A closure binding that runs on first touch instead of on resolution
$container->lazy(PdfReportGenerator::class, fn(Container $c) => new PdfReportGenerator(
$c->get(Database::class),
));The first two forms produce the same lazy ghost as the attribute. The closure
form produces a lazy proxy instead — the closure, not the constructor, makes
the instance — but it is still a native lazy object of the right class, so
instanceof and ::class behave exactly as above.
The target must be a concrete, instantiable class either way: a lazy instance has
to be an instance of something. lazy(SomeInterface::class) with no concrete
throws a ContainerException.
A callable factory must return a non-lazy object compatible with the registered class's native proxy. Returning a subclass is incompatible, even if it adds no properties. First use raises ContainerException naming the registration and returned type. Same-class results work; an ancestor result works only when the proxy adds no properties and changes neither __clone() nor __destruct(). See PHP's proxy rules.
Register the actual concrete result class for a subtype factory, then bind the base name to it:
$container->lazy(SesMailer::class, fn () => new SesMailer());
$container->bind(Mailer::class, SesMailer::class);Registering both #[Lazy] and lazy() for the same class is not an error, and
singleton() combines with it the way #[Singleton] #[Lazy] does — one shared
instance, constructed on first touch. A scope inherits the lazy
registrations its parent had when the scope was created.
An uninitialized lazy instance keeps the container that produced it alive, since first touch is what resolves its dependencies — the one thing that outlives the container reference the caller drops. Touching it releases the hold. This matters only if you hand lazy instances out and drop the container while they are still untouched; see disposal.
See bindings.
#[Singleton] #[Lazy]: one shared instance for direct reads and constructor injection, constructed on first touch#[Factory] #[Lazy]— a fresh lazy instance per resolution
A contextual class target keeps its singleton lifetime even when a global binding
replaces that class for other consumers. Selecting Heavy::class explicitly
shares the cached Heavy instance, and forget(Heavy::class) clears that cache.
Scopes share an inherited singleton registration unless they shadow its binding;
forgetting it on the parent clears the shared instance for subsequent lookups.
Stored scope registrations and aliases still take precedence. When a parent owns
the selected singleton, resolution hooks run from that parent through every
intermediate scope to the requesting scope.
Native lazy objects require PHP 8.4. On 8.3 a #[Lazy] class is constructed
eagerly instead. That is unobservable apart from the timing, so the attribute is
safe to use across both.
Callable factories are also eager on PHP 8.3, so native proxy class restrictions do not apply there.