3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Feature { public function getClass() { $func = function (self $instance) { print get_class($instance); }; $func($this); } } class Foo { use Feature; } class Bar { use Feature; } (new Foo)->getClass(); (new Bar)->getClass();
Output for 7.1.20 - 7.1.33, 7.2.6 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.27, 8.0.0 - 8.0.15, 8.1.0 - 8.1.2
FooBar
Output for 7.1.0 - 7.1.13, 7.2.0 - 7.2.1
Foo Fatal error: Uncaught TypeError: Argument 1 passed to Bar::{closure}() must be an instance of Foo, instance of Bar given, called in /in/oKeSG on line 10 and defined in /in/oKeSG:6 Stack trace: #0 /in/oKeSG(10): Bar->{closure}(Object(Bar)) #1 /in/oKeSG(26): Bar->getClass() #2 {main} thrown in /in/oKeSG on line 6
Process exited with code 255.

preferences:
53.2 ms | 529 KiB | 12 Q