w00
w00

Reputation: 26792

Use variables inside an anonymous function, which is defined somewhere else

When using anonymous functions in PHP, you can easily use variables from right outside of its scope by using the use() keyword.

In my case the anonymous functions are already defined somewhere, but called later on (somewhere else) in a class.

The following piece of code is to illustrate the idea:

<?php

$bla = function ( $var1 ) use ($arg)
        {
            echo $var1;
        };

class MyClass
{
    private $func;

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

    public function test ( $arg )
    {
        $closure =  $this->func;
        $closure ( 'anon func' );
    }
}

$c = new MyClass($bla);
$c->test ( 'anon func' );

What i'm doing is i create an anonymous function and store that in a variable. I pass that variable to the method of a class and that is where i want to run the anonymous function.

But i can't use the use() keyword to get the $arg parameter from the method this way. Because the anonymous function was declared outside of the method.

But i really need a way to get the variables from the method where the anonymous function is run from. Is there a way to do that, when the anonymous function is declared somewhere else..?

Upvotes: 13

Views: 12645

Answers (3)

Jon Hulka
Jon Hulka

Reputation: 1319

Building on @chiliNUT answer, rather than making $arg global, pass it by reference to the MyClass constructor:

class MyClass
{
    private
        $func,
        $arg;

    public function __construct ( $func , &$arg )
    {
        $this->func = $func;
        $this->arg = &$arg;
    }

    public function test ( $v )
    {
        $this->arg = $v;
        $closure =  $this->func;
        $closure ( 'anon func' );
    }
}

class RunIt
{
    public function run()
    {
        $arg = null;
        $bla = function($var1) use (&$arg)
        {
            echo $var1 . ' : ' . $arg;
        };
        $c = new MyClass($bla, $arg);
        $c->test ( 'this is arg' );
    }
}

(new RunIt())->run();

If you want to allow any number of use parameters:

class MyClass
{
    private
        $func,
        $args;

    public function __construct ( $func , &...$args )
    {
        $this->func = $func;
        $this->args = &$args;
    }

    public function test ( ...$vs )
    {
        foreach($vs as $i => $v) if($i < count($this->args)) $this->args[$i] = $v;
        $closure =  $this->func;
        $closure ( 'anon func' );
    }
}

class RunIt
{
    public function run()
    {
        $arg1 = '';
        $arg2 = '';
        $arg3 = '';
        $bla = function($var1) use (&$arg1, &$arg2, &$arg3)
        {
            echo $var1 . ' : ' . $arg1 . ' : ' . $arg2 . ' : ' . $arg3;
        };
        $c = new MyClass($bla, $arg1, $arg2, $arg3);
        $c->test ( 'this is arg1', 'this is arg2' ); //$arg3 retains its previous value
    }
}

(new RunIt())->run();

Upvotes: 0

chiliNUT
chiliNUT

Reputation: 19573

FWIW, you can do it if you use a use reference (php.net ex 3.3) and a global, ugly since it uses globals, but just to put it out there:

<?php
$bla = function ( $var1 ) use (&$arg)
        {
            return "var1:$var1, arg:$arg";
        };

class MyClass
{
    private $func;

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

    public function test ( $param )
    {
        global $arg;
        $arg=$param;
        $closure =  $this->func;
        return  $closure ( 'anon func' );
    }
}

$c = new MyClass($bla);
echo $c->test ( 'bla bla' ); //var1:anon func, arg:bla bla

Upvotes: 2

Gordon
Gordon

Reputation: 317119

The point of the use keyword is to inherit/close over a particular environment state from the parent scope into the Closure when it's defined, e.g.

$foo = 1;

$fn = function() use ($foo) {
    return $foo;
};

$foo = 2;

echo $fn(); // gives 1

If you want $foo to be closed over at a later point, either define the closure later or, if you want $foo to be always the current value (2), pass $foo as a regular parameter.

Upvotes: 23

Related Questions