Reputation: 1587
While learning about anonymous functions in PHP I came across this:
Anonymous functions can use the variables defined in their enclosing scope using the use syntax.
For example:
$test = array("hello", "there", "what's up");
$useRandom = "random";
$result = usort($test, function($a, $b) use ($useRandom){
if($useRandom=="random")
return rand(0,2) - 1;
else
return strlen($a) - strlen($b);
}
);
Why can't I just make $useRandom global like below?
$test2 = array("hello", "there", "what's up");
$useRandom = "random";
$result = usort($test, function($a, $b){
global $useRandom;
if($useRandom=="random")
return rand(0,2) - 1;
else
return strlen($a) - strlen($b);
}
);
What's the difference between these two approaches?
Upvotes: 3
Views: 206
Reputation: 34426
You have to be careful with globals for many reasons, not the least of which is unexpected behavior:
$foo = 'Am I a global?';
function testGlobal(){
global $foo;
echo $foo . ' Yes, I am.<br />';
}
function testAgain() {
if(isset($foo)) {
echo $foo . ' Yes, I am still global';
} else {
echo 'Global not available.<br />';
}
}
testGlobal();
testAgain();
In this example you might expect that once you have made the variable global it would be available to subsequent functions. The result of running this code demonstrates that this is not the case:
Am I a global? Yes, I am.
Global not available.
Declaring a global in this way makes the variable available within the scope of its global declaration (in this case, the function testGlobal()
), but not in the general scope of the script.
Check out this answer on Using Globals and why it is a bad idea.
Upvotes: 2
Reputation: 29932
Your examples are a bit simplified. To get the difference try to wrap your example code into another function, thus creating an additional scope around the inner callback, that is not global.
In the following example $useRandom
is always null
in the sorting callback, since there is no global variable called $useRandom
. You will need to use use
to access a variable from an outer scope that is not the global scope.
function test()
{
$test = array( "hello", "there", "what's up" );
$useRandom = "random";
$result = usort( $test, function ( $a, $b ) {
global $useRandom;
// isset( $useRandom ) == false
if( $useRandom == "random" ) {
return rand( 0, 2 ) - 1;
}
else {
return strlen( $a ) - strlen( $b );
}
}
);
}
test();
On the other hand, if there is a global variable $useRandom
it can be accessed using use
only one scope down. In the next example $useRandom
is again null
, since it was defined two scopes "higher" and the use
keyword only imports variables from the scope directly outside the current scope.
$useRandom = "random";
function test()
{
$test = array( "hello", "there", "what's up" );
$result = usort( $test, function ( $a, $b ) use ( $useRandom ) {
// isset( $useRandom ) == false
if( $useRandom == "random" ) {
return rand( 0, 2 ) - 1;
}
else {
return strlen( $a ) - strlen( $b );
}
}
);
}
test();
Upvotes: 4