I want to store anonymous functions in an array inside a class. Below is my working codes. But I don't understand why I must pass in values in the second argument while I have already declare it to be an empty array by default if nothing is set from outside.
-- public function methods($method = "init",$options = array()){--
Any ideas?
class myclass {
public function __construct() {}
public function methods($method = "init",$options = array()){
$methods = array(
"init" => function($options){
return $options;
},
"run" => function($options){
return $options;
},
);
return call_user_func_array( $methods[$method], $options);
}
}
$obj = new myclass();
var_dump($obj->methods("init",array("hello world!"))); // string 'hello world!' (length=12) --> correct.
var_dump($obj->methods("init",array())); // Warning: Missing argument 1 for myclass::{closure}() in...refer to -> "init" => function($options){
var_dump($obj->methods("init")); // Warning: Missing argument 1 for myclass::{closure}() in...refer to -> "init" => function($options){
I thought it should return these as results,
var_dump($obj->methods("init",array())); // array (size=0) empty
var_dump($obj->methods("init")); // array (size=0) empty