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

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.

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