3v4l.org

run code in 300+ PHP versions simultaneously
<?php readonly class FormattedString { private string $value; private function __construct (string $str) { $this->value = $str; } public static function create (string $str): self|NotValidFormattedString { $matches = null; preg_match("/[a-z]{2}-[a-z]{2}-[a-z]{8}-[a-z]{1}/", $str, $matches); if (count($matches) === 0) { return new NotValidFormattedString($str, "poshel nahoi suka, chto za govno ti mne peredal, blyad"); } return new self($str); } } readonly class NotValidFormattedString { public function __construct (public string $str, public string $err) {} } class ValidateFormattedString { public function __invoke(string $str): FormattedString|NotValidFormattedString { return FormattedString::create($str); } } class SuperYellowCircle { public function __construct (public FormattedString $toChtoVMoemPrimereByloClassomYoba) {} } $stringOne = (new ValidateFormattedString)("aa-bb-cccccccc-d"); $circleOne = new SuperYellowCircle($stringOne); var_dump($circleOne); $stringTwo = (new ValidateFormattedString)("butthurt"); $circleTwo = new SuperYellowCircle($stringTwo); var_dump($circleTwo);
Output for 8.2.22 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
object(SuperYellowCircle)#1 (1) { ["toChtoVMoemPrimereByloClassomYoba"]=> object(FormattedString)#2 (1) { ["value":"FormattedString":private]=> string(16) "aa-bb-cccccccc-d" } } Fatal error: Uncaught TypeError: SuperYellowCircle::__construct(): Argument #1 ($toChtoVMoemPrimereByloClassomYoba) must be of type FormattedString, NotValidFormattedString given, called in /in/pJbBI on line 40 and defined in /in/pJbBI:32 Stack trace: #0 /in/pJbBI(40): SuperYellowCircle->__construct(Object(NotValidFormattedString)) #1 {main} thrown in /in/pJbBI on line 32
Process exited with code 255.
Output for 8.1.30 - 8.1.33
Parse error: syntax error, unexpected token "readonly", expecting end of file in /in/pJbBI on line 2
Process exited with code 255.

preferences:
63.78 ms | 407 KiB | 5 Q