dsds33222 2019-07-14 11:42
浏览 542
已采纳

PHP - 将匿名函数作为参数传递

Is it possible to pass an anonymous function as a parameter in PHP? And if yes - how?

I am trying to pass an anonymous function to a setter which will fill an array with values returned from that function.

class MyClass
{
    private $arr = array();

    public function __construct()
    {
        $this->setArrElm('New', function(){return 123;});
    }

    private function setArrElm($name, $val)
    {
        // here: gettype($val) == object
        $this->arr[$name] = $val;
    }
}

Please note the comment - the type of val is object and I expect an int.

  • 写回答

1条回答 默认 最新

  • dongtong5242 2019-07-14 12:06
    关注

    In PHP 7 you can self execute the closure

    class MyClass
    {
        private $arr = array();
    
        public function __construct()
        {
            $this->setArrElm('New', (function(){return 123;})()); //<-- self execute
        }
    
        private function setArrElm($name, int $val) //<-- added typehint
        {
            // here: gettype($val) == object
            $this->arr[$name] = $val;
            print_r($val);
        }
    }
    
    new MyClass;
    

    Output

    123
    

    Sandbox

    This takes a form similar to JS (probably other languages too):

     (function(){return 123;})()
    

    It's important to know that it's executing the function, then passing the result. You can pass the closure (which is an object) and then execute it, too. But if you have strict types and need an int, you can self execute the closure too.

    It really only makes sense to do this if you need an int as the argument. Even in that case you can execute it beforehand and then pass the result. This just saves you a local variable.

    For < PHP7 or just because

    Alt1

    class MyClass
    {
        private $arr = array();
    
        public function __construct()
        {
            $var = function(){return 123;};
    
            $this->setArrElm('New', $var()); //<-- execute
        }
    
        private function setArrElm($name, $val) //<-- added typehint
        {
            // here: gettype($val) == object
            $this->arr[$name] = $val;
            print_r($val);
        }
    }
    
    new MyClass;
    

    Alt2

    class MyClass
    {
        private $arr = array();
    
        public function __construct()
        {
            $var = function(){return 123;};
    
            $this->setArrElm('New', $var); 
        }
    
        private function setArrElm($name, $val) //<-- mixed
        {
            if(gettype($val) == 'object' && is_a($val, '\Closure')){
                 //is a closure, you could use is_callable etc. too. see __invoke()
    
                $val = $val();
            }
            $this->arr[$name] = $val;
            print_r($val);
        }
    }
    
    new MyClass;
    

    Alt3

    class MyClass
    {
        private $arr = array();
    
        public function __construct()
        {
            $var = function(){return 123;};
    
            $this->setArrElm('New', $var); 
        }
    
        private function setArrElm($name, $val) //<-- mixed
        {
            if(is_callable($val)){
                //pass functions (as a string) or arrays or closures(executable classes with __invoke)
                $val = call_user_func($val);
            }
            $this->arr[$name] = $val;
            print_r($val);
        }
    }
    
    new MyClass;
    

    Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效