3v4l.org

run code in 300+ PHP versions simultaneously
<?php class StringCastable { private $data; public function __constructor($data) { $this->data = $data; } public function __toString() { return $this->toString(); } public function toString() { return $this->data; } } function testStringCast(string $data) { var_dump($data, get_class($data)); } testStringCast(new StringCastable("speculoos"));
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught TypeError: StringCastable::__toString(): Return value must be of type string, null returned in /in/GI3Y7:14 Stack trace: #0 /in/GI3Y7(23): StringCastable->__toString() #1 /in/GI3Y7(28): testStringCast(Object(StringCastable)) #2 {main} thrown in /in/GI3Y7 on line 14
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Method StringCastable::__toString() must return a string value in /in/GI3Y7:23 Stack trace: #0 /in/GI3Y7(28): testStringCast(Object(StringCastable)) #1 {main} thrown in /in/GI3Y7 on line 23
Process exited with code 255.
Output for 7.1.0 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33
Recoverable fatal error: Method StringCastable::__toString() must return a string value in /in/GI3Y7 on line 23
Process exited with code 255.
Output for 7.0.0 - 7.0.20
Catchable fatal error: Method StringCastable::__toString() must return a string value in /in/GI3Y7 on line 23
Process exited with code 255.
Output for 5.4.8 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Catchable fatal error: Argument 1 passed to testStringCast() must be an instance of string, instance of StringCastable given, called in /in/GI3Y7 on line 28 and defined in /in/GI3Y7 on line 23
Process exited with code 255.

preferences:
202.87 ms | 402 KiB | 248 Q