Inject lazy objects by the container
Lazy dependency injection allows for deferring the creation of an object's dependencies until the moment they are actually used—and not a moment sooner. To implement this functionality, the container utilizes "Virtual Proxies," as described in the PHP documentation here.
IMPORTANT
The mechanism for implementing lazy dependencies is available only for PHP 8.4 and higher.
TIP
If the PHP version is below 8.4, you can declare a lazy dependency injection using the diProxyClosure() helper function or the ProxyClosure() attribute.
How to use
Imagine that the PHP class \App\Bar requires significant computational resources and memory during initialization, thereby increasing the time it takes to create an object of that class.
The container will inject the PHP class \App\Bar into the constructor of the PHP class \App\Foo.
<?php
declare(strict_types=1);
namespace App;
final class Foo
{
public function __construct(
private readonly Bar $bar,
private readonly Baz $baz,
) {}
public function doWithBar(): mixed
{
// using the `$this->bar` dependency here
}
public function doWithBaz(): mixed
{
// using the `$this->baz` dependency here
}
}As the code shows, the constructor of the PHP class \App\Foo will receive two injected objects via the container whenever $container->get(App\Foo::class) is called, including the resource intensive class \App\Bar, even though it is not always used.
Declaring a lazy injection for the PHP class \App\Bar resolves the issue of instantiating resource intensive dependencies.
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.
<?php
declare(strict_types=1);
namespace App;
use Kaspi\DiContainer\Attributes\Autowire;
final class Foo
{
public function __construct(
#[Autowire(isLazy: true)]
private readonly Bar $bar,
private readonly Baz $baz,
) {}
public function doWithBar(): mixed
{
// using the `$this->bar` dependency here
}
public function doWithBaz(): mixed
{
// using the `$this->baz` dependency here
}
}The initialization of the \App\Bar PHP class, configured as a lazy injection, will occur only when the private property $bar of the \App\Foo class is accessed specifically, when the \App\Foo::doWithBar() method is called and not before. This reduces memory consumption, resource usage, and the time required to initialize the \App\Foo PHP class.