PHP class declaration
In cases where a PHP class cannot be instantiated simply by calling new Foo(), but instead requires a specific construction process, you can delegate the creation of that class to a dedicated PHP class, known as a “factory”.
Parameters for the constructor and methods of a PHP factory class can be injected automatically based on the container configuration. For specific scenarios, you can declare arguments for the factory method.
A PHP class can be defined via a factory in the following ways:
- using the
diFactory()helper function within configuration files. - using the
\Kaspi\DiContainer\Attributes\DiFactoryattribute.
Important note: the factory method[1] must return the class being defined, thereby enabling access to that PHP class via the container's get() and has() methods.
Example of declaring a PHP class via a factory
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Qux.php
namespace App\Services;
final class Qux
{
public function __construct(public readonly string $value) {}
}<?php
declare(strict_types=1);
// file: /app/src/Helpers/Configurator.php
namespace App\Helpers;
use App\Services\Bar;
final class Configurator
{
public function __construct(public readonly Bar $bar) {}
public function doConfigQux(): Qux
{
$calculatedValue = $this->bar->calculateSomething();
return '' === $calculatedValue
? new Qux('Lorem ipsum')
: new Qux($calculatedValue);
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
public function __construct(
public readonly Baz $baz
) {}
public function calculateSomething(): string
{
if (0 < $this->baz->calculateSomething()) {
return 'Fuuz';
}
return 'Ok';
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Baz.php
namespace App\Services;
use function rand;
final class Baz
{
// …
public function calculateSomething(): int
{
// here are some calculations
return rand(-100, 100); // test data
}
}Register the PHP class \App\Services\Qux in the container using the factory \App\Helpers\Configurator::quxDoConfig().
Declaration in configuration files
In the services.php configuration file, we will register the PHP class \App\Services\Qux in the container using the diFactory() helper function.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Qux;
$container = (new DiContainerBuilder())
->load('/app/config/services.php')
->build();
var_dump($container->has(Qux::class));
var_dump($container->get(Qux::class)->value);<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\Qux;
use function Kaspi\DiContainer\diFactory;
return static function (): Generator {
yield Qux::class => diFactory([Configurator::class, 'doConfigQux']);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
(bool) true
string(4) "Fuuz"Declaration via an attribute
Register the PHP class \App\Services\Qux in the container using the \Kaspi\DiContainer\Attributes\DiFactory attribute.
<?php
declare(strict_types=1);
// file: /app/src/Services/Qux.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\DiFactory;
use App\Helpers\Configurator;
#[DiFactory([Configurator::class, 'doConfigQux'])]
final class Qux
{
public function __construct(public readonly string $value) {}
}<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Qux;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
var_dump($container->has(Qux::class));
var_dump($container->get(Qux::class)->value);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
(bool) true
string(4) "Fuuz"In some scenarios, it becomes necessary—for the purpose of dependency injection—to employ the "factory" design pattern, delegating the creation of a required object to a dedicated PHP class known as a "factory." ↩︎