Declaring the injection of a lazy PHP class into a parameter
Declaring a PHP class as "lazy" for injection into a method parameter or a callable type means that a proxy object will be injected specifically for that parameter; in all other cases, for example, when retrieving the object via the container's get() method, the actual PHP class instance will be returned, with all its dependencies already injected.
A PHP class can be declared as "lazy" using the diAutowire() helper function or the \Kaspi\DiContainer\Attributes\Autowire attribute by setting the $isLazy parameter to true. When a PHP class is declared as lazy, a proxy object is injected, which implements deferred (lazy) initialization of the actual object.
Example of implementing a lazy PHP class
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/FooService.php
namespace App\Services;
final class FooService
{
public function __construct(
public readonly HeavyDependency $heavyDependency
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/BarService.php
namespace App\Services;
final class BarService
{
public function __construct(
public readonly HeavyDependency $heavyDependency
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/HeavyDependency.php
namespace App\Services;
final class HeavyDependency
{
public function __construct(
public readonly BazService $bazService
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/BazService.php
namespace App\Services;
final class BazService
{
/*
* A class that requires a long initialization time
* and involves resource-intensive computations.
*/
}For the PHP class \App\Services\FooService, the dependency \App\Services\HeavyDependency must be injected into the constructor parameter $heavyDependency via a proxy object (lazy object). For all other PHP classes, the dependency \App\Services\HeavyDependency should be injected without a proxy object.
Declaration in configuration files
In the services.php configuration file, register the PHP class \App\Services\FooService using the diAutowire() helper function, and configure the injection of the \App\Services\HeavyDependency PHP class into the $heavyDependency constructor parameter as a lazy object.
<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\{FooService, BarService, HeavyDependency};
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->load('/app/config/services.php')
->build();
var_dump($container->get(FooService::class));
echo PHP_EOL;
var_dump($container->get(BarService::class));
echo PHP_EOL;
var_dump($container->get(HeavyDependency::class));<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Services\{FooService, HeavyDependency};
use function Kaspi\DiContainer\diAutowire;
return static function (): Generator {
yield diAutowire(FooService::class)
->bindArguments(
heavyDependency: diAutowire(
HeavyDependency::class,
isLazy: true,
)
);
};Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
object(App\Services\FooService)#23 (1) {
["heavyDependency"]=>
lazy proxy object(App\Services\HeavyDependency)#20 (0) {
["bazService"]=>
uninitialized(App\Services\BazService)
}
}
object(App\Services\BarService)#33 (1) {
["heavyDependency"]=>
object(App\Services\HeavyDependency)#30 (1) {
["bazService"]=>
object(App\Services\BazService)#27 (0) {
}
}
}
object(App\Services\HeavyDependency)#40 (0) {
["bazService"]=>
object(App\Services\BazService)#37 {
}
}Declaration via an attribute
Declare the $heavyDependency constructor parameter of the PHP class \App\Services\FooService — which is of type \App\Services\HeavyDependency – as a lazy object, using the \Kaspi\DiContainer\Attributes\Autowire attribute:
<?php
declare(strict_types=1);
// file: /app/src/Services/FooService.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Autowire;
final class FooService
{
public function __construct(
#[Autowire(isLazy: true)]
public readonly HeavyDependency $heavyDependency
) {}
// …<?php
declare(strict_types=1);
// file: /app/src/container_build.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\{FooService, BarService, HeavyDependency};
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
var_dump($container->get(FooService::class));
echo PHP_EOL;
var_dump($container->get(BarService::class));
echo PHP_EOL;
var_dump($container->get(HeavyDependency::class));Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
object(App\Services\FooService)#23 (1) {
["heavyDependency"]=>
lazy proxy object(App\Services\HeavyDependency)#20 (0) {
["bazService"]=>
uninitialized(App\Services\BazService)
}
}
object(App\Services\BarService)#33 (1) {
["heavyDependency"]=>
object(App\Services\HeavyDependency)#30 (1) {
["bazService"]=>
object(App\Services\BazService)#27 (0) {
}
}
}
object(App\Services\HeavyDependency)#40 (0) {
["bazService"]=>
object(App\Services\BazService)#37 {
}
}