3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); final readonly class Version { private string $version; private function __construct( string $version ){ // TODO: ensure is valid $this->version = $version; } public static function fromString(string $string): self { return new self($string); } } final readonly class Example { private ?Version $modifiedVersionForCloning = null; public function __construct( public Version $version ) {} public function __clone() { $this->version = $this->modifiedVersionForCloning; $this->modifiedVersionForCloning = null; } public function lol(string $version): self { $this->modifiedVersionForCloning = Version::fromString($version); $clone = clone $this; return $clone; } } $example = new Example(Version::fromString('8.2')); var_dump($example->lol('8.3'));
Output for 8.2.0 - 8.2.16, 8.3.0 - 8.3.5
Fatal error: Readonly property Example::$modifiedVersionForCloning cannot have default value in /in/DJhth on line 24
Process exited with code 255.
Output for 8.1.0 - 8.1.27
Parse error: syntax error, unexpected token "readonly", expecting "abstract" or "final" or "class" in /in/DJhth on line 5
Process exited with code 255.

preferences:
42.47 ms | 699 KiB | 3 Q