Luminova Framework

PHP Luminova: Lazy Load Object Trait Initialization

Last updated: 2025-11-02 11:23:29

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.

// /app/Utils/Example.php

namespace App\Utils;

use Luminova\Utility\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\Utility\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.

The initializer receives the raw ghost ($this) and optional arguments.It can:

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

Parameters:

ParameterTypeDescription
$initializercallableThe class initializer.
Function Signature callable(static $ghost, mixed ...$args): static|null|void
$argumentscallable|nullOptional callback providing init args.
Function Signature callable(): array.

Return Value:

static - Return unconstructed ghost instance.

Throws:

Examples:

$ghost = Example::newLazyGhost(function (Example $obj, string $name) {
    return new Example('Alice');
});

echo $ghost->getName(); // Alice
$ghost = Example::newLazyGhost(function (Example $obj, string $name) {
    $obj->name = $name; // set private data
});

$ghost->hydrateLazyGhost(['Alice']); // manually hydrate before use
$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.

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

Parameters:

ParameterTypeDescription
$argumentsarray|nullOptional arguments to override lazy initialization arguments.

Throws:

Example:

$ghost = Example::newLazyGhost(fn(Example $obj): void => $obj->age = 42);

$ghost->hydrateLazyGhost(); // runs initializer

echo $ghost->getAge(); // 42

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.

final public static newLazyProxy(
    callable $initializer, 
    ?callable $arguments = null
): \Luminova\Interface\LazyObjectInterface<static>

Parameters:

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

Return Value:

\Luminova\Interface\LazyObjectInterface&lt;static&gt; - Proxy object that initializes on first use

Throws:

Example:

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

echo $proxy->getData(); // object is created on first call

Note:The initializer must return instance of class.