3v4l.org

run code in 300+ PHP versions simultaneously
<?php class OrderItem { public int $order_id; public string $sku; public int $quantity; } class OrderItems { static function get(int $order_id):array { static $times = 0; if ( 0 !== $times ) { trigger_error('If this happens then the second call to callAndSet() evaluated the closure again.'); $times = 1; } // Simulating a two item order $item = new OrderItem(); $item->order_id = $order_id; $item->sku = 'ABC-123'; $item->quantity = 12; $item2 = new OrderItem(); $item2->order_id = $order_id; $item2->sku = 'XYZ-987'; $item2->quantity = 5; return [$item,$item2]; } } function callAndSet(mixed &$lazy) { return $lazy instanceof Closure ? $lazy = $lazy() : $lazy; } class Order { public int $order_id; public Closure|array $order_items; function __construct(int $order_id) { $this->order_id = $order_id; $this->order_items = fn() => OrderItems::get($order_id); } } $order = new Order(123); foreach( callAndSet($order->order_items) as $order_item) { printf("%d of %s\n", $order_item->quantity, $order_item->sku); } echo "---\n"; foreach( callAndSet($order->order_items) as $order_item) { printf("%d of %s\n", $order_item->quantity, $order_item->sku); } echo "---\n"; foreach( $order->order_items as $order_item) { printf("%d of %s\n", $order_item->quantity, $order_item->sku); }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0150.00618.43
8.3.50.0090.01216.52
8.3.40.0110.01118.72
8.3.30.0160.00018.75
8.3.20.0040.00424.18
8.3.10.0000.00724.66
8.3.00.0050.00326.16
8.2.180.0030.01225.92
8.2.170.0140.00419.08
8.2.160.0100.00322.96
8.2.150.0040.00425.66
8.2.140.0080.00024.66
8.2.130.0080.00026.16
8.2.120.0070.00021.13
8.2.110.0090.00020.46
8.2.100.0060.00620.38
8.1.280.0090.00625.92
8.1.270.0050.00323.99
8.1.260.0050.00326.35
8.1.250.0040.00428.09
8.1.240.0050.00520.39
8.1.230.0080.00321.99
8.1.100.0070.00517.36
8.0.80.0080.00916.98
7.4.300.0020.00516.42
7.4.190.0030.00516.34

preferences:
45.61 ms | 401 KiB | 5 Q