Luminova Framework

PHP Luminova: Lazy Load Object Trait Initialization

Last updated: 2026-11-28 20:57:12

The lazy object provides a proxy for lazily instantiating an object. The actual object is not initialized until one of its methods or properties is accessed.

Use LazyObjectTrait to create a ghost or a proxy for a class. A ghost is an instance of the class that can be hydrated explicitly. A proxy forwards public method and property access to an object created by an initializer.

// /app/Utils/Example.php

namespace App\Utils;

use Luminova\Components\Object\LazyObjectTrait;

class Example
{
    use LazyObjectTrait;

    public string $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getName(): string
    {
        return $this->name;
    }
}

Class Definition

  • Trait namespace: \Luminova\Components\Object\LazyObjectTrait

Methods

newLazyGhost

Create a lazy ghost instance of this class.

A ghost is an object created without running its constructor.This is useful if you need to fill private or protected properties manuallybefore the object is used.

On hydration, constructor arguments are passed to the class constructor. The initializer then receives the ghost itself.It can:

  • Mutate $this and return nothing, OR
  • Return a fully constructed instance.
public static final function newLazyGhost(callable $initializer, ?callable $arguments = null): static

Parameters:

ParameterTypeDescription
$initializercallableThe class initializer.
Receives the constructed ghost.
$arguments?callableOptional callback that supplies constructor arguments.

Return:

static - Return unconstructed ghost instance.

Throws:

Examples:

$ghost = Example::newLazyGhost(
    fn (Example $obj): Example => new Example(strtoupper($obj->name)),
    fn (): array => ['Alice']
);

$ghost->hydrateLazyGhost();
$ghost = Example::newLazyGhost(function (Example $obj): void {
    $obj->name = strtoupper($obj->name);
});

$ghost->hydrateLazyGhost(['Alice']); // manually hydrate before use
echo $ghost->getName(); // ALICE

hydrateLazyGhost

Manually trigger hydration of a lazy ghost object.

If you plan to call a public method immediately, call this first to ensurethe ghost has been initialized. If the initializer returns an object, it is stored;otherwise we assume $this was mutated in place.

public final function hydrateLazyGhost(?array $arguments = null): void

Parameters:

ParameterTypeDescription
$arguments?array<array-key,mixed>Optional arguments to override lazy initialization arguments.

Throws:

Example:

$ghost = Example::newLazyGhost(function (Example $obj): void {
    $obj->name .= ' Smith';
});

$ghost->hydrateLazyGhost(['Alice']);

echo $ghost->getName(); // Alice Smith

newLazyProxy

Create a lazy proxy instance of this class.

A proxy defers real object creation until the first time you access it.Unlike ghosts, proxies require the initializer to return a fully constructed object.This wrapper only exposes the public API of your class.

public static final function newLazyProxy(callable $initializer, ?callable $arguments = null): object

Parameters:

ParameterTypeDescription
$initializercallableMust return an instance.
Function Signature callable(static $proxy, mixed ...$args): static
$arguments?callableOptional callback providing initialization arguments.
Function Signature callable(): array

Return:

object - Proxy object that initializes on first use

Throws:

Example:

$proxy = Example::newLazyProxy(fn(): Example => new Example('data'));

echo $proxy->getName(); // data

Note:The initializer must return an instance of the class that created the proxy.