3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo {} class Bar { public function firstTypeError ($passed) { $trace = debug_backtrace(); throw new TypeError(__CLASS__ . "::" . __FUNCTION__ . "(): Argument #1 (\$passed) elements must be of type " . Baz::class . ", " . get_debug_Type($passed) . " given, called in " . $trace[0]["file"] . " on line " . $trace[0]["line"]); } public function secondTypeError ($passed) { $trace = debug_backtrace(); throw new TypeError(__CLASS__ . "::" . __FUNCTION__ . "(): Argument #1 (\$passed) elements must be of type " . Baz::class . ", " . get_debug_Type($passed) . " given"); } } class Baz {} $bar = new Bar(); try { $bar->firstTypeError("test"); } catch (TypeError $e) { echo $e->__toString(); } echo "\n\n\n\n"; try { $bar->secondTypeError("test"); } catch (TypeError $e) { echo $e->__toString(); }
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
TypeError: Bar::firstTypeError(): Argument #1 ($passed) elements must be of type Baz, string given, called in /in/vR4M1 on line 22 and defined in /in/vR4M1:8 Stack trace: #0 /in/vR4M1(22): Bar->firstTypeError('test') #1 {main} TypeError: Bar::secondTypeError(): Argument #1 ($passed) elements must be of type Baz, string given in /in/vR4M1:13 Stack trace: #0 /in/vR4M1(30): Bar->secondTypeError('test') #1 {main}

preferences:
68.27 ms | 407 KiB | 5 Q