PHP 8.5.0 RC 2 available for testing

Voting

: min(five, two)?
(Example: nine)

The Note You're Voting On

dave1010 at gmail dot com
10 months ago
Simple helper function that makes it easier to understand:

<?php

function createLazyGhost(
string $class,
?callable
$initializer = null,
?array
$propertySetterCallables = null
): object {
$reflection = new ReflectionClass($class);

return
$reflection->newLazyGhost(function (object $object) use ($initializer, $propertySetterCallables) {
// Initialize via the main initializer if provided
if ($initializer) {
$initializer($object);
}

// Set properties using the callables if provided
if ($propertySetterCallables) {
foreach (
$propertySetterCallables as $property => $callable) {
if (
is_callable($callable)) {
$object->$property = $callable();
}
}
}
});
}

?>

This supports using either a main object initializer and/or property initializers.

Here's an example, where generating order IDs and calculating totals is considered expensive, so we only do it when necessary:

<?php

class Order {
public
string $orderId = '';
public
float $total = 0.0;
}

$initializer = function (Order $order) {
$order->orderId = 'ORD12345';
};

$propertySetters = [
'total' => fn() => 200.75,
];

// Lazy ghost with both an initializer and property callables
$lazyOrder = createLazyGhost(Order::class, $initializer, $propertySetters);

// We can now use $lazyOrder as normal, even though the properties haven't been calculated yet.

// Do something that triggers initialization
echo $lazyOrder->orderId . PHP_EOL;
echo
$lazyOrder->total . PHP_EOL;

?>

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /