3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait ClosureMethods { function __get($method){ return function(...$args) use($method){ return $this->$method(...$args); }; } } class Foo { use ClosureMethods; function baz(int $a, int $b){ return $a + $b; } } $foo = new Foo; $methodRef = $foo->baz; var_dump($methodRef(10, 20));
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
int(30)
Output for 5.6.0 - 5.6.28
Catchable fatal error: Argument 1 passed to Foo::baz() must be an instance of int, integer given, called in /in/kkO0C on line 6 and defined in /in/kkO0C on line 13
Process exited with code 255.
Output for 5.5.0 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/kkO0C on line 6
Process exited with code 255.

preferences:
98.8 ms | 1617 KiB | 4 Q