0

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.

3
  • 1
    When passing an anonymous function like that, PHP will actually pass an object that represents the function. It's called a Closure. It's a callable object so you can still use it as a function. Commented Jul 14, 2019 at 11:43
  • $this->arr[$name] = $val(); Commented Jul 14, 2019 at 11:47
  • Possible duplicate of PHP: Pass anonymous function as argument Commented Jul 14, 2019 at 11:55

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.