tags:

views:

64

answers:

3

Is it possible to access outer local varialbe in a PHP sub-function?

In below code, I want to access variable $l in inner function bar. Declaring $l as global $l in bar doesn't work.

function foo()
{
    $l = "xyz";

    function bar()
    {
        echo $l;
    }
    bar();
}
foo();
+4  A: 

I don't think this is possible with PHP 5.2...

With PHP 5.3, though, you could probably use a Closure, to do just that...


Edit : took some time to remember the syntax, but here's what it would look like :

function foo()
{
    $l = "xyz";
    $bar = function () use ($l)
    {
        var_dump($l);
    };
    $bar();
}
foo();

And, running the script, you'd get :

$ php temp.php
string(3) "xyz"


A couple of note :

  • You must put a ; after the function's declaration !
  • You could use the variable by reference, with a & before it's name : use (& $l)

For more informations, as a reference, you can take a look at this page in the manual : Anonymous functions

Pascal MARTIN
Why do you use nested functions at all?
FractalizeR
Who knows ^^ That wasn't the question ^^ ;; with PHP 5.3 and Anonymous functions, it can be pretty useful for Closures ;;; Before PHP 5.3... I've never used any nested function with PHP < 5.3.
Pascal MARTIN
+1  A: 

In PHP 5.3 you must use use keyword.

$bar = function() use(&$l) {
};
$bar();

In PHP 5.2 and earlier this won't work. The syntax you've got isn't a closure, but definition of a global function.

function foo() { function bar() { } }

works the same as:

function foo() { include "file_with_function_bar.php"; }

If you execute function foo twice, PHP will complain that you've tried to re-define (global) function bar.

porneL
Too many pronouns! What will die on second execution? The outer-variable, or the variable as it is `use`'d in the inner function? And what does die mean (not being snarky, just want to know). Does die mean the variable can't be used or that it has to be re-established by some other means? I'm guessing it dies (in whatever sense) because it the shared location they both point at can't be re-pointed at after first execution? What if the variable is made static and then unset right afterward?
Anthony
die means `die("Fatal Error")`. There are no closures nor inner functions in PHP <= 5.2 – function inside function is not allowed, and the fact it isn't syntax error is accidental (it should be sytnax error).PHP 5.3 is entirely different. It allows functions inside functions, but with slightly different syntax. Once you get syntax right, in PHP 5.3+, it will work as you'd expect in other languages.
porneL
+1  A: 

Just passing it along as an argument to your inner function isn't a solution?

Mark
This might be one of those "how would one do it" type puzzles where something far more complex might have justified not passing it to the second function. Plus what if they want to change the semi-global variable (like a counter) from within the function but have that value sustain outside the function (maybe they don't want to deal with passing it back at the end as they have enough to return?) Just thinkin out loud.
Anthony