Possible Duplicate:
Anonymous functions that execute immediately
javascript code:
(function(){
var msg = 'Hello World';
console.log(msg);
})();
Is there an equivalent way to do this in php?
Possible Duplicate:
Anonymous functions that execute immediately
javascript code:
(function(){
var msg = 'Hello World';
console.log(msg);
})();
Is there an equivalent way to do this in php?
The pattern in your example is used to overcome issues with function-level static scoping in javascript. There is no equivalent pattern in PHP, since PHP has no such issues.
You might as well just create a new class.
It is possible:
call_user_func(function() {
$localvar = 'foo';
echo $localvar;
});
And while I agree that it has less value in PHP than in JS, there are use cases, i.e. procedural include files. For further description of the method, see my blog.