Actual name of the factory class
Typically, the PHP class factory[1] is represented by two string values – the name of the PHP class and the name of the method of this class:
$factoryDefinition = [App\Services\Foo::class, 'methodName'];To obtain the result of a call to a PHP "factory" class, the first step is to retrieve an object via the container's get('App\Services\Foo') method; only then is the factory method methodName() invoked.
It follows from this that a string value representing the name of a PHP class serves as a container identifier[2].
Any container identifier may be specified as the name of a factory class, provided that the object retrieved from the container implements the factory method.
Example of specifying a container identifier as a factory class name
Using the container identifier 'services.foo_special', you can retrieve an instance of the PHP class \App\Services\Foo that has been configured in a specific way.
You need to declare a dependency injection via the factory method \App\Services\Foo::doBaz() into the $value constructor parameter of the PHP class \App\Services\Bar.
<?php
declare(strict_types=1);
namespace App\Services;
final class Bar
{
public function __construct(public readonly mixed $value) {}
}The configuration file custom_services.php defines a specific configuration for the PHP class \App\Services\Foo and a container identifier 'services.foo_special':
<?php
declare(strict_types=1);
// file: /app/config/custom_services.php
use App\Services\Foo;
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield 'services.foo_special' => diAutowire(Foo::class, isSingleton: true)
->setup('methodOne')
->setup('methodTwo');
};Declaration in configuration files
In the services.php configuration file, register the PHP class \App\Services\Bar in the container and configure dependency injection for the constructor parameter $value using the diFactory() helper function.
<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Service\Bar;
use function Kaspi\DiContainer\{diAutowire, diFactory};
return static function (): Generator {
yield diAutowire(Bar::class)
->bindArguments(
value: diFactory(['services.foo_special', 'doBaz'])
);
};Declaration via an attribute
Declare the injection of a value into the $value constructor parameter of the PHP class \App\Services\Bar using the \Kaspi\DiContainer\Attributes\DiFactory attribute.
<?php
declare(strict_types=1);
namespace App\Services;
use Kaspi\DiContainer\Attributes\DiFactory;
final class Bar
{
public function __construct(
#[DiFactory(['services.foo_special', 'doBaz'])]
public readonly mixed $value
) {}
}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." ↩︎
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. ↩︎