PHP Luminova: Lazy Load Object Trait Initialization
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): staticParameters:
| Parameter | Type | Description |
|---|---|---|
$initializer | callable | The class initializer. Function Signature callable(static $ghost, mixed ...$args): static|null|void |
$arguments | callable|null | Optional callback providing init args. Function Signature callable(): array. |
Return Value:
static - Return unconstructed ghost instance.
Throws:
- \Luminova\Exceptions\ExceptionsRuntimeException - if error while invoking class method.
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(); // AlicehydrateLazyGhost
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): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$arguments | array|null | Optional arguments to override lazy initialization arguments. |
Throws:
- \Luminova\Exceptions\ExceptionsRuntimeException - if error while initializing class.
Example:
$ghost = Example::newLazyGhost(fn(Example $obj): void => $obj->age = 42);
$ghost->hydrateLazyGhost(); // runs initializer
echo $ghost->getAge(); // 42newLazyProxy
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:
| Parameter | Type | Description |
|---|---|---|
$initializer | callable | Must return an instance. Function Signature callable(static $proxy, mixed ...$args): static |
$arguments | callable|null | Optional callback providing initialization arguments. Function Signature callable(): array |
Return Value:
\Luminova\Interface\LazyObjectInterface<static> - Proxy object that initializes on first use
Throws:
- \Luminova\Exceptions\RuntimeException - if error while invoking class method.
Example:
$proxy = Example::newLazyProxy(fn(): Example => new Example('data'));
echo $proxy->getData(); // object is created on first callNote:The initializer must return instance of class.