Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php oop:在类内的数组中生成匿名函数_Php_Oop_Anonymous Function - Fatal编程技术网

php oop:在类内的数组中生成匿名函数

php oop:在类内的数组中生成匿名函数,php,oop,anonymous-function,Php,Oop,Anonymous Function,我想在类中的数组中存储匿名函数。下面是我的工作代码。但是我不明白为什么我必须在第二个参数中传入值,而我已经在默认情况下声明它为空数组,如果外部没有设置任何内容 -公共函数方法$method=init,$options=array{- 有什么想法吗 class myclass { public function __construct() {} public function methods($method = "init",$options = array()){

我想在类中的数组中存储匿名函数。下面是我的工作代码。但是我不明白为什么我必须在第二个参数中传入值,而我已经在默认情况下声明它为空数组,如果外部没有设置任何内容

-公共函数方法$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

您的案例的正确结案声明是:

$methods = array(
    "init" => function() use ($options){
        return $options;
    },

    "run" => function() use ($options){
        return $options;
    },
);
如果在闭包声明中包含$options变量,其作用域将是闭包本身。因此,将创建一个新的$options变量,并覆盖外部函数中的$options变量

通过使用use关键字,您可以告诉PHP您想要使用当前启动的传递的$options变量

现在,您可以省略call\u user\u func\u数组调用中的第二个参数,如下所示

call_user_func_array($methods[$method])

您的案例的正确结案声明是:

$methods = array(
    "init" => function() use ($options){
        return $options;
    },

    "run" => function() use ($options){
        return $options;
    },
);
如果在闭包声明中包含$options变量,其作用域将是闭包本身。因此,将创建一个新的$options变量,并覆盖外部函数中的$options变量

通过使用use关键字,您可以告诉PHP您想要使用当前启动的传递的$options变量

现在,您可以省略call\u user\u func\u数组调用中的第二个参数,如下所示

call_user_func_array($methods[$method])

然后更改为call_user_func然后更改为call_user_func