3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Animal{ private $name; public function __construct($name){ $this->name = $name; } public function say($say_func=null){ $say_func = is_callable($say_func)? $say_func: function(){ var_dump('default',$this); }; $say_func(); return $say_func; } } $b = function(){ var_dump('custom',$this); }; $a = new Animal('bob'); $f= $a->say(); $a->say($b); $f();
Output for 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
string(7) "default" object(Animal)#2 (1) { ["name":"Animal":private]=> string(3) "bob" } Fatal error: Uncaught Error: Using $this when not in object context in /in/8AifB:21 Stack trace: #0 /in/8AifB(15): {closure}() #1 /in/8AifB(26): Animal->say(Object(Closure)) #2 {main} thrown in /in/8AifB on line 21
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20
string(7) "default" object(Animal)#2 (1) { ["name":"Animal":private]=> string(3) "bob" } Notice: Undefined variable: this in /in/8AifB on line 21 string(6) "custom" NULL string(7) "default" object(Animal)#2 (1) { ["name":"Animal":private]=> string(3) "bob" }

preferences:
184.09 ms | 402 KiB | 242 Q