Use local variable of a function to its nested function in PHP

With PHP 5.3, you could probably use a Closure,
You could use use keyword to pass parameter.

Method 1.

function outer()
{
    $l = "xyz";
    $inner = function () use ($l)
    {
        var_dump($l);
    };
    $inner();
}
outer();

Method 2:

function outer()
{
    $l = "...";
    function inner($l)
    {
        // print $l
    }
    inner($l);
}
outer();

You can use any method, you found easy.


0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment