3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); final class SomeStringable { public function __toString(): string { echo "__toString executed" . PHP_EOL; return "foo"; } } final class SomeClass { public function __construct(public ?string $value) {} } $nullableVar = new SomeStringable(1); //$nullableVar = null; //$result = new SomeClass(null === $nullableVar ? null : (string) $nullableVar); // works $result = new SomeClass($nullableVar); // Fatal error var_dump($result->value);
Output for 8.1.23 - 8.1.34, 8.2.0 - 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
Fatal error: Uncaught TypeError: SomeClass::__construct(): Argument #1 ($value) must be of type ?string, SomeStringable given, called in /in/OBatI on line 20 and defined in /in/OBatI:13 Stack trace: #0 /in/OBatI(20): SomeClass->__construct(Object(SomeStringable)) #1 {main} thrown in /in/OBatI on line 13
Process exited with code 255.

preferences:
27.34 ms | 1152 KiB | 7 Q