3v4l.org

run code in 300+ PHP versions simultaneously
<?php function foo(int $a) { var_dump($a); } function define_callbacks() { $string = 'foo'; $lambda = fn(...$args) => foo(...$args); $fcc = foo(...); return [$string, $lambda, $fcc]; } function test_callback($callback) { $callback(1); try { $callback(new class{}); } catch ( TypeError $e ) { echo $e->getMessage(), "\n"; } } [$string, $lambda, $fcc] = define_callbacks(); echo "== String-based ==\n"; test_callback($string); echo "\n== Type-less lambda ==\n"; test_callback($lambda); echo "\n== First-class callable ==\n"; test_callback($fcc);
Output for 8.2.3
== String-based == int(1) foo(): Argument #1 ($a) must be of type int, class@anonymous given, called in /in/Q2H2Z on line 18 == Type-less lambda == int(1) foo(): Argument #1 ($a) must be of type int, class@anonymous given, called in /in/Q2H2Z on line 9 == First-class callable == int(1) foo(): Argument #1 ($a) must be of type int, class@anonymous given, called in /in/Q2H2Z on line 18

preferences:
221.34 ms | 1399 KiB | 8 Q