Declaration via attributes
To instruct the container on how to inject dependencies into PHP classes or callable types[1], a mechanism for declaring definitions via attributes is used.
In the container configuration, the $useAttribute parameter is enabled by default.
To create a configured container, the builder class DiContainerBuilder is used.
Attributes contain metadata for declaring container definitions: arguments, injection via setter methods, or the declaration of tags.
When declaring container definitions, you can combine attributes and configuration files.
WARNING
Defining container entries or specifying dependency injections for method parameters via attributes takes precedence over definitions made in configuration files.
IMPORTANT
To declare a PHP class or interface, you must choose only one declaration method: either via attributes or through configuration files.
Available attributes
- Autowire – declaring a PHP class in a container.
- AutowireExclude – excludes from the container's injected dependencies.
- DiFactory – dependency injection via the factory pattern.
- Inject – dependency injection by container identifier[2].
- InjectByCallable – dependency injection via a call to a
callabletype. - Parameter – dependency injection from "container parameters".
- ParameterRuntime – dependency injection from "runtime container parameters".
- ProxyClosure – implementation of "lazy" dependency.
- Service – specifies the implementation of a PHP interface.
- Setup – dependency injection into a PHP class via a mutable setter method.
- SetupImmutable – Dependency injection into a PHP class via an immutable setter method.
- SetupPriority – setter method priority.
- Tag – tag declaration.
- TaggedAs – a collection by tag.
- DiRuntime – implement "runtime definition".
The
callabletype represents values or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎A container ID is a string name (or a class/interface name) used to register a service or object within a container and to retrieve it via the
get()method. ↩︎