<?php
$obj = new StdClass;
$obj->fn = function($arg) { return "Hello $arg"; };
echo ($obj->fn)('World');
class Bla {
public $fn;
public $fn2;
public function __construct($f){
$this->fn2 = $f;
}
}
$bla = new Bla;
$bla->fn = function($arg="Bu"){return "Hello lo $arg"; }; // can't do in class!
echo ($bla->fn)('Wa');
$blah = new Bla(function(){ return "F!";});
echo ($bla->fn2)();
Hello World
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Bla::__construct(), 0 passed in /in/bOkjT on line 15 and exactly 1 expected in /in/bOkjT:11
Stack trace:
#0 /in/bOkjT(15): Bla->__construct()
#1 {main}
thrown in /in/bOkjT on line 11
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Hello World
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Bla::__construct(), 0 passed in /in/bOkjT on line 15 and exactly 1 expected in /in/bOkjT:11
Stack trace:
#0 /in/bOkjT(15): Bla->__construct()
#1 {main}
thrown in /in/bOkjT on line 11
Process exited with code 255.
Output for 7.0.0 - 7.0.20
Hello World
Warning: Missing argument 1 for Bla::__construct(), called in /in/bOkjT on line 15 and defined in /in/bOkjT on line 11
Notice: Undefined variable: f in /in/bOkjT on line 12
Hello lo Wa
Fatal error: Uncaught Error: Function name must be a string in /in/bOkjT:20
Stack trace:
#0 {main}
thrown in /in/bOkjT on line 20
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /in/bOkjT on line 5
Process exited with code 255.