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.
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): staticParameters:
| Parameter | Type | Description |
|---|---|---|
$initializer | callable | The class initializer. Receives the constructed ghost. |
$arguments | ?callable | Optional callback that supplies constructor arguments. |
Return:
static - Return unconstructed ghost instance.
Throws:
- Luminova\Exceptions\RuntimeException - if error while invoking class method.
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(); // 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.
public final function hydrateLazyGhost(?array $arguments = null): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$arguments | ?array<array-key,mixed> | Optional arguments to override lazy initialization arguments. |
Throws:
- Luminova\Exceptions\RuntimeException - if error while initializing class.
Example:
$ghost = Example::newLazyGhost(function (Example $obj): void {
$obj->name .= ' Smith';
});
$ghost->hydrateLazyGhost(['Alice']);
echo $ghost->getName(); // Alice SmithnewLazyProxy
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): objectParameters:
| Parameter | Type | Description |
|---|---|---|
$initializer | callable | Must return an instance. Function Signature callable(static $proxy, mixed ...$args): static |
$arguments | ?callable | Optional callback providing initialization arguments. Function Signature callable(): array |
Return:
object - 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->getName(); // dataNote:The initializer must return an instance of the class that created the proxy.