tags:

views:

33

answers:

5

Imagine

aFunction() {
    $foo = 123;
    // a lot more code
}

aFunction();
echo $foo; // undefined

Is there a way to put the procedure of aFunction into the scope of the caller?

A: 

You can use global.

aFunction() {
    global $foo, $bar, $etc;
    $foo = 123;
    $bar = 456;
    $etc = 789;
    // a lot more code
}

aFunction();
echo $foo . $bar . $etc; // 123456789

or

aFunction() {
    $foo = 123;
    // a lot more code
    return $foo;
}

echo aFunction(); // 123
Tor Valamo
global is notoriously evil. I'd redesign/refactor my system before I used global.
Alex
Sure, but that wasn't the question.
Tor Valamo
Also, global is only evil if you use PHP for your entire app instead of just templating (for which it was originally intended).
Tor Valamo
A: 

It is possible by using global variables, but I would say that it is a Very Bad Idea. There are a couple of ways to go about doing this though:

you can take the return value of aFunction

function aFunction() {

    return 'hello!';

}

$foo = aFunction();
echo $foo; // output: hello!

or pass $foo in by reference and have aFunction work on its value:

function aFunction(&$foo) {

    $foo = 'hello!';

}

$foo = '';
aFunction($foo);
echo $foo; // output: hello!

I'm guessing that since you want to just use the variables in the calling scope, that the return value solution isn't plausible since there are many of them. In this case, the reference idea works, but an even better solution would be to wrap the variables you need in a class:

class Foo {

    public $foo;
    public $bar;
    public $baz;

}

function aFunction(Foo $foo) {

    $foo->foo = 'hello';
    $foo->bar = 'there';
    $foo->baz = 'mate';

    return $foo;

}

$foo = new Foo;
$foo = aFunction($foo);
echo "{$foo->foo} {$foo->bar} {$foo->baz}"; // output: hello there mate

That way, you can use the class in other functions as well and it logically groups those variables together. Better practice would be to modify and fetch those variables through class methods, in case you ever need to perform some kind of processing on the variables before they are set / returned. You can use the pass-by-reference method with a class too, but it is best to stick to return values where possible, since variables getting modified when they are passed to a function can be an unexpected side effect.

This also reduces the number of parameters to your function, which turns the Very Bad Idea into a Great Thing.

Carson Myers
+1  A: 

No, it's not possible.

You could put something extremely ugly together with get_defined_vars and extract but even that would give you only the values, not the actual variables.

Try to work with global if necessary. Most often, the intended goal can be reached in a cleaner way.

Pekka
+2  A: 

No,

In order to get the value out of it you'd have to use one of the following:

  1. have aFunction return the value
  2. have the value passed into aFunction by reference
  3. use a global
McAden
A: 

PHP 5.3 seems to support closures.

http://www.ibm.com/developerworks/opensource/library/os-php-5.3new2/index.html

You won't find many PHP developers who actually know what a closure is, so it's not surprising that they're not very popular.

Azeem.Butt