3v4l.org

run code in 300+ PHP versions simultaneously
<?php function mimicking($obj, Closure $exec){ $thisVal = (new ReflectionFunction($exec))->getClosureThis(); return ($exec)->bindTo($thisVal, $obj)(); } class A{ protected $stuff = 10; function test(){ return new class($this){ function __construct(A $s){ $this->s = $s; } function asd(){ return mimicking($this->s, function(){ return $this->s->stuff; }); } }; } } $x = new A; echo $x->test()->asd();
Output for 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Deprecated: Creation of dynamic property class@anonymous::$s is deprecated in /in/CLaVs on line 13 10
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 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.27
10
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /in/CLaVs on line 5
Process exited with code 255.

preferences:
184.47 ms | 401 KiB | 224 Q