Skip to content

Setup attribute ​

The \Kaspi\DiContainer\Attributes\Setup attribute injects dependencies into a PHP class via mutable setter methods[1].

Attribute configuration:

  • \Attribute::TARGET_METHOD
  • \Attribute::IS_REPEATABLE

Attribute signature:

php
\Kaspi\DiContainer\Attributes\Setup::__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 a callable type.
  • 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.


  1. A mutable setter method is a method used to change the value of an object's internal property; it modifies the existing object in place and typically returns void. ↩︎