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

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?