3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Rectangle { public int $area { get => $this->height * $this->width; } public function __construct( public int $height, public int $width, ) {} public function __serialize(): array { $properties = new ReflectionClass($this)->getProperties(); return array_reduce($properties, fn (array $values, ReflectionProperty $property): array => [ ...$values, $property->getName() => $property->getValue($this), ], []); } public function __unserialize(array $values): void { $properties = new ReflectionClass($this)->getProperties(); foreach ($properties as $property) { $value = $values[$property->getName()]; $property->setValue($this, $value); } } } $instance = new Rectangle(4, 5); $serialized = serialize($instance); var_dump($serialized); $unserialized = unserialize($serialized); var_dump($unserialized);
Output for 8.4.1
string(69) "O:9:"Rectangle":3:{s:4:"area";i:20;s:6:"height";i:4;s:5:"width";i:5;}" Fatal error: Uncaught Error: Property Rectangle::$area is read-only in /in/LRdao:32 Stack trace: #0 /in/LRdao(32): ReflectionProperty->setValue(Object(Rectangle), 20) #1 [internal function]: Rectangle->__unserialize(Array) #2 /in/LRdao(42): unserialize('O:9:"Rectangle"...') #3 {main} thrown in /in/LRdao on line 32
Process exited with code 255.
Output for 8.2.0 - 8.2.26, 8.3.0 - 8.3.14
Parse error: syntax error, unexpected token "{", expecting "," or ";" in /in/LRdao on line 5
Process exited with code 255.

preferences:
171.43 ms | 1010 KiB | 7 Q