3v4l.org

run code in 300+ PHP versions simultaneously
<?php final readonly class Post { public int $id; public string $name; public string $email; public function greet(): string { return "Hello 👋"; } } final readonly class PostRepository { private ReflectionClass $reflector; public function __construct( ) { $this->reflector = new ReflectionClass(Post::class); } public function find(int $id): Post { var_dump("Loading post with ID $id"); $entity = $this->reflector->newLazyGhost(function ($entity) use ($id) { var_dump("Lazy initialization triggered for ID $id"); $data = $this->loadFromDatabase($id); $this->reflector->getProperty('name')->setValue($entity, $data['name']); $this->reflector->getProperty('email')->setValue($entity, $data['email']); }); // ID is already known and can be accessed without triggering initialization $this->reflector->getProperty('id')->setRawValueWithoutLazyInitialization($entity, $id); return $entity; } private function loadFromDatabase(int $id): array { // Simulate loading from database return [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', ]; } } $em = new PostRepository(); $post = $em->find(42); var_dump($post); var_dump($post->greet()); // This will really trigger the lazy initialization $post->email; var_dump($post);

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.5.00.0120.01019.99
8.4.150.0030.00014.05
8.4.140.0130.01017.77
8.4.130.0100.01219.02
8.4.120.0130.00723.89
8.4.110.0120.00722.57
8.4.100.0060.00417.60
8.4.90.0100.00518.88
8.4.80.0080.01117.81
8.4.70.0100.00917.76
8.4.60.0120.00818.61
8.4.50.0090.00718.65
8.4.40.0200.00017.53
8.4.30.0160.00320.32
8.4.20.0100.01019.20
8.4.10.0090.00019.50
8.3.280.0140.00718.43
8.3.270.0110.00916.95
8.3.260.0100.00516.79
8.3.250.0070.00218.82
8.3.240.0100.01017.02
8.3.230.0140.00716.60
8.3.220.0030.00417.10
8.3.210.0060.00316.82
8.3.200.0060.00316.63
8.3.190.0130.00616.76
8.3.180.0060.00416.70
8.3.170.0060.01317.11
8.3.160.0120.00619.16
8.3.150.0110.00417.09
8.3.140.0090.00016.86
8.3.130.0250.01117.14
8.3.120.0000.00818.65
8.3.110.0040.00416.85
8.3.50.0040.00416.70
8.2.290.0090.00920.49
8.2.280.0100.00916.53
8.2.270.0000.01616.52
8.2.260.0120.00417.18
8.2.250.0080.00816.89
8.2.240.0070.00318.60
8.2.230.0090.00016.60
8.1.330.0080.01023.48
8.1.320.0130.00616.22
8.1.310.0060.00916.84
8.1.300.0090.00016.13

preferences:
30.82 ms | 403 KiB | 5 Q