АSetupImmutable attribute
The \Kaspi\DiContainer\Attributes\SetupImmutable attribute injects dependencies into a PHP class via immutable setter methods [1].
The return value of such a setter method must be self, static, or the same PHP class to which the setter method belongs.
Attribute configuration:
\Attribute::TARGET_METHOD\Attribute::IS_REPEATABLE
Attribute signature:
\Kaspi\DiContainer\Attributes\SetupImmutable::__construct(
mixed ...$argument
)Parameters:
$argument- arguments to be passed to a setter method.
Setter method arguments
The variable-length parameter $argument passes arguments to the setter method.
When declaring dependency injection for a specific parameter of a setter method, you can use named arguments.
Setter method parameters that do not receive arguments will be injected by the container automatically, based on the configuration.
You can use classes from other container definitions within the arguments.
Kaspi\DiContainer\DiDefinition\DiDefinitionAutowire– PHP class.Kaspi\DiContainer\DiDefinition\DiDefinitionCallable– injects a dependency by invoking acallabletype.Kaspi\DiContainer\DiDefinition\DiDefinitionFactory– factory for dependency injection.Kaspi\DiContainer\DiDefinition\DiDefinitionGet– dependency injection via a reference to a container identifier.Kaspi\DiContainer\DiDefinition\DiDefinitionParameter– dependency injection from container parameters.Kaspi\DiContainer\DiDefinition\DiDefinitionParameterRutime– dependency injection from runtime container parameters.Kaspi\DiContainer\DiDefinition\DiDefinitionProxyClosure– dependency injection via "lazy loading".Kaspi\DiContainer\DiDefinition\DiDefinitionRuntime– runtime object injection.Kaspi\DiContainer\DiDefinition\DiDefinitionTaggedAs– dependency injection - collection by tag.
An example of using setter methods for dependency injection can be found in the Cookbook section.
Priority
The container injects dependencies via setter methods marked with an attribute, following the order in which they are declared in the PHP class.
If necessary, you can change the order of dependency injection using the Kaspi\DiContainer\Attributes\SetupPriority attribute.
An immutable setter method is a method that does not modify the current object, but instead creates and returns a new instance of it with an updated value. ↩︎