Dependency injection via setter methods
Dependency injection into a PHP class can be performed via setter methods.
You can configure dependency injection via setter methods using the setup() or setupImmutable() methods, provided by the diAutowire() helper function, or by using the \Kaspi\DiContainer\Attributes\Setup and \Kaspi\DiContainer\Attributes\SetupImmutable attributes.
Mutable setter method[1]
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class Foo
{
private QuxInterface $qux;
public function setQux(QuxInterface $qux): void
{
$this->qux = $qux;
}
public function getQux(): ?QuxInterface
{
return $this->qux ?? null;
}
}<?php
declare(strict_types=1);
// file: /app/src/Interfaces/QuxInterface.php
namespace App\Interfaces;
interface QuxInterface
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class Bar implements QuxInterface
{
// …
}You need to configure the injection of the PHP class \App\Services\Bar via the mutable setter method \App\Services\Foo::setQux().
The $qux parameter of the \App\Services\Foo::setQux() method expects an object that implements the \App\Interfaces\QuxInterface interface. The container provides several ways to define the injection of a PHP class into a parameter; for this example, we will use either the helper function diGet() or the attribute \Kaspi\DiContainer\Attributes\Inject, depending on the chosen method for defining the dependency injection.
Declaration in configuration files
In the services.php configuration file, we will define the PHP class \App\Services\Foo and specify injection via the setter method 'setQux'.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->load('/app/config/services.php')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->getQux());<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Service\{Foo, Bar};
use function Kaspi\DiContainer\{diAutowire, diGet};
return static function (): Generator {
yield diAutowire(Foo::class)
->setup(
'setQux',
[
'qux' => diGet(Bar::class),
],
);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
object(App\Services\Bar)#23 (0) {
}Declaration via an attribute
We will declare the injection of the PHP class \App\Services\Bar using the \Kaspi\DiContainer\Attributes\Setup attribute, specifying it for the setter method \App\Services\Foo::setQux(). To declare the injection into a parameter of the setter method, we use the \Kaspi\DiContainer\Attributes\Inject attribute.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use App\Interfaces\QuxInterface;
use Kaspi\DiContainer\Attributes\Inject;
use Kaspi\DiContainer\Attributes\Setup;
final class Foo
{
private QuxInterface $qux;
public function setQux(QuxInterface $qux): void
#[Setup]
public function setQux(
#[Inject(Bar::class)]
QuxInterface $qux
): void
{
// …<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->getQux());Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
object(App\Services\Bar)#23 (0) {
}Immutable setter method[2]
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class Baz
{
private QuxInterface $qux;
public function withQux(QuxInterface $qux): self
{
$new = clone $this;
$new->qux = $qux;
return $new;
}
public function getQux(): ?QuxInterface
{
return $this->qux ?? null;
}
}<?php
declare(strict_types=1);
// file: /app/src/Interfaces/QuxInterface.php
namespace App\Interfaces;
interface QuxInterface
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class Bar implements QuxInterface
{
// …
}You need to configure the injection of the PHP class \App\Services\Bar via the immutable setter method \App\Services\Baz::withQux(). Note that this setter method returns a new instance of the \App\Services\Baz class.
The $qux parameter of the \App\Services\Baz::withQux() method expects an object that implements the \App\Interfaces\QuxInterface interface. The container offers various ways to define the injection of a PHP class into a parameter; for this example, you can use either the diGet() helper function or the \Kaspi\DiContainer\Attributes\Inject attribute, depending on the chosen method for defining the dependency injection.
Declaration in configuration files
In the services.php configuration file, we will define the PHP class \App\Services\Baz and specify dependency injection via the setter method 'withQux'.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Baz;
$container = (new DiContainerBuilder())
->load('/app/config/services.php')
->build();
$baz = $container->get(Baz::class);
var_dump($baz->getQux());<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Service\{Baz, Bar};
use function Kaspi\DiContainer\{diAutowire, diGet};
return static function (): Generator {
yield diAutowire(Baz::class)
->setupImmutable(
'withQux',
[
'qux' => diGet(Bar::class),
],
);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
object(App\Services\Bar)#23 (0) {
}Declaration via an attribute
Let's declare the implementation of the PHP class \App\Services\Bar through the attribute \Kaspi\DiContainer\Attributes\SetupImmutable, specifying it for the setter method \App\Services\Baz::withQux(). To declare injection into a setter method parameter, use the \Kaspi\DiContainer\Attributes\Inject attribute.
<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Services;
use App\Interfaces\QuxInterface;
use Kaspi\DiContainer\Attributes\Inject;
use Kaspi\DiContainer\Attributes\SetupImmutable;
final class Baz
{
private QuxInterface $qux;
public function withQux(QuxInterface $qux): self
#[SetupImmutable]
public function withQux(
#[Inject(Bar::class)]
QuxInterface $qux
): void
{
// …<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Baz;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$baz = $container->get(Baz::class);
var_dump($baz->getQux());Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
object(App\Services\Bar)#23 (0) {
}Injecting multiple dependencies
Let’s look at an example involving the injection of multiple dependencies via a single mutable[1:1] setter method: \App\Services\Rules::addRule().
To declare dependency injection, we use the \Kaspi\DiContainer\Attributes\Setup attribute.
The $inputRule parameter of the setter method expects an object that implements the \App\Interfaces\RuleInterface interface. The container provides several ways to declare the injection of a PHP class into a parameter; for this example, we will use the \Kaspi\DiContainer\DiDefinition\DiDefinitionGet PHP class.
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.
<?php
declare(strict_types=1);
// file: /app/src/container.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Rules;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src/')
->build();
$rules = $container->get(Rules::class);
foreach($rules->getRules() as $rule) {
var_dump($rule);
print PHP_EOL;
}<?php
declare(strict_types=1);
// file: /app/src/Services/Rules.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Setup;
use Kaspi\DiContainer\DiDefinition\DiDefinitionGet as DiGet;
use App\Rules\{RuleA, RuleB};
use App\Interfaces\RuleInterface;
class Rules
{
private array $rules = [];
// …
#[Setup(
new DiGet(RuleB::class)
)]
#[Setup(
new DiGet(RuleA::class)
)]
public function addRule(RuleInterface $inputRule): void
{
$this->rules[] = $inputRule;
}
public function getRules(): array
{
return $this->rules;
}
}<?php
declare(strict_types=1);
// file: /app/src/Interfaces/RuleInterface.php
namespace App\Interfaces;
interface RuleInterface
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Rules/RuleA.php
namespace App\Rules;
use App\Interfaces\RuleInterface;
class RuleA implements RuleInterface
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Rules/RuleB.php
namespace App\Rules;
use App\Interfaces\RuleInterface;
class RuleB implements RuleInterface
{
// …
}Run the following command in the terminal:
php /app/src/container.phpA message like the following will appear:
object(App\Rules\RuleB)#2 (0) {
}
object(App\Rules\RuleA)#2 (0) {
}The order in which objects are injected via a setter method is determined by the sequence in which the \Kaspi\DiContainer\Attributes\Setup attribute is applied to the \App\Services\RuleGenerator::addRule() method.
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. ↩︎ ↩︎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. ↩︎