我想将匿名函数存储在类内的数组中。以下是我的工作代码。但是我不明白为什么我必须在第二个参数中传入值,而如果没有从外部设置任何内容,我已经默认将其声明为空数组。
-- public function methods($method = "init",$options = array()){
--
有任何想法吗?
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){
我认为它应该将这些作为结果返回,
var_dump($obj->methods("init",array())); // array (size=0) empty
var_dump($obj->methods("init")); // array (size=0) empty