Storing a Closure Function in a Class Property in PHP

后端 未结 6 1033
清歌不尽
清歌不尽 2021-01-21 07:37

ok I do have the code below

bar();
       }
    }

    $mee         


        
6条回答
  •  感情败类
    2021-01-21 08:21

    Your example code at codepad.org does not work because codepad.org uses PHP 5.2.5, and closure support was only added in 5.3.

    However, your code will also not work in a PHP version that supports closures, although you will get a different error: http://codepad.viper-7.com/Ob0bH5

    This is a limitation of PHP at present. $obj->member() looks for a method named member and will not look at properties to see if they are callable. It is, frankly, annoying.

    The only way I am aware of to make this work without call_user_func()/call_user_func_array() is:

    public function boo() {
       $func = $this->bar;
       $func();
    }
    

提交回复
热议问题