Skip to content

Declaring a lazy PHP class in a container ​

Declaring a PHP class as "lazy" directly within the container means that, whenever it is injected, whether as a method parameter or into a callable type[1], it is provided as a proxy object; the actual initialization of the PHP class occurs only at the moment it is accessed.

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 a lazy PHP class declaration ​

PHP classes:

php
<?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
<?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
<?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
<?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.
         */
}

The PHP class \App\Services\HeavyDependency must be registered in the container so that it is injected into method parameters, wherever it is used, as a "lazy" object, without requiring any additional declarations.

A lazy instance of \App\Services\HeavyDependency should be injected into the $heavyDependency constructor parameter of the PHP classes \App\Services\FooService and \App\Services\BarService.

Declaration in configuration files ​

In the lazy_services.php configuration file, declare the lazy PHP class \App\Services\HeavyDependency using the diAutowire() helper function, with the $isLazy parameter set to true:

php
<?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/lazy_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
<?php
declare(strict_types=1);

// file: /app/config/lazy_services.php

use App\Services\HeavyDependency;

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield diAutowire(
        HeavyDependency::class,
        isLazy: true,
    );
};

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
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"]=>
  lazy proxy object(App\Services\HeavyDependency)#30 (0) {
    ["bazService"]=>
    uninitialized(App\Services\BazService)
  }
}

lazy proxy object(App\Services\HeavyDependency)#40 (0) {
  ["bazService"]=>
  uninitialized(App\Services\BazService)
}

Declaration via an attribute ​

Declare a lazy PHP class \App\Services\HeavyDependency using the \Kaspi\DiContainer\Attributes\Autowire attribute, with the $isLazy constructor parameter set to true:

php
<?php
declare(strict_types=1);

// file: /app/src/Services/HeavyDependency.php

namespace App\Services;

use Kaspi\DiContainer\Attributes\Autowire;

#[Autowire(isLazy: true)]
final class HeavyDependency
{
// …
php
<?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:

shell
php /app/src/container_build.php

A message like the following will appear:

console
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"]=>
  lazy proxy object(App\Services\HeavyDependency)#30 (0) {
    ["bazService"]=>
    uninitialized(App\Services\BazService)
  }
}

lazy proxy object(App\Services\HeavyDependency)#40 (0) {
  ["bazService"]=>
  uninitialized(App\Services\BazService)
}

  1. The callable type represents values ​​or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎