create_function
.
Since then, I brought up Wez’ original patch again on the php-internals lists. This has prompted some hefty debate over the last week. The main argument against approving the patch, seems to be, that one would expect static scoping rules to apply to the anonymous function. After all, this is the case in similar languages, which support anonymous functions. One could only assume, that changing PHP to support this, would be a major undertaking.
One would be wrong, it seems.
Today, Christian Seiler posted a patch to allow lexical scope for anonymous functions. There are a few loose ends, but it appears to work in general. Instead of making all variables follow the static scoping rules, a new keyword (lexical
) is introduced. It works similar to global
, in that it must be explicitly declared, which variables are lexically scoped. This is how it looks in action:
function getAdder($x) {
return function ($y) {
lexical $x;
return $x + $y;
};
}
$add2 = getAdder(2);
$add2(8); // return 10
It’s probably a bit early to tell if this will find its way into the language. It’s still just a proposal and it would take some further work to get it right, but at least it appears to be technically possible. We’ll have to wait and see.
Otherwise, there’s just left to wish you all a merry Christmas.
Frequently Asked Questions on Lexical Scope in PHP
What is the difference between lexical scope and dynamic scope?
Lexical scope, also known as static scoping, is a method of variable scoping where variables are accessible within the region they are defined. This is the scoping method used in PHP. On the other hand, dynamic scoping is a method where variables are accessible from where they are called, not where they are defined. This is less common and not used in PHP.
How does lexical scoping work in PHP?
In PHP, lexical scoping works by allowing variables to be accessible within the region they are defined. This means that if a variable is defined within a function, it can only be accessed within that function. However, PHP also supports closures which allow variables to be used outside the function they are defined in.
What is a closure in PHP?
A closure in PHP is an anonymous function that can access variables from the parent scope, even if the parent function has finished execution. This is made possible by the ‘use’ keyword, which imports variables from the parent scope into the closure’s scope.
How does the ‘use’ keyword work in PHP?
The ‘use’ keyword in PHP is used to import variables from the parent scope into the closure’s scope. This allows the closure to access these variables even after the parent function has finished execution. The ‘use’ keyword is followed by the variables to be imported, enclosed in parentheses.
Can you provide an example of lexical scoping in PHP?
Sure, here’s a simple example of lexical scoping in PHP:function outerFunction() {
$x = 1;
function innerFunction() {
echo $x; // This will result in an error
}
innerFunction();
}
outerFunction();
In this example, the variable $x is defined in the outerFunction and is not accessible in the innerFunction due to lexical scoping.
What is the difference between global and local scope in PHP?
In PHP, a variable declared outside a function has a global scope and can be accessed anywhere in the script. However, a variable declared within a function has a local scope and can only be accessed within that function.
How can I access a global variable inside a function in PHP?
To access a global variable inside a function in PHP, you can use the ‘global’ keyword. This will import the global variable into the function’s scope. For example:$x = 1; // global scope
function test() {
global $x; // importing the global variable
echo $x; // This will output 1
}
test();
What is variable shadowing in PHP?
Variable shadowing in PHP occurs when a variable declared within a function has the same name as a variable in the parent scope. The inner variable “shadows” the outer variable, making it inaccessible.
Can a function in PHP access variables from its parent function?
By default, a function in PHP cannot access variables from its parent function due to lexical scoping. However, this can be achieved using closures and the ‘use’ keyword.
How does lexical scoping affect performance in PHP?
Lexical scoping can improve performance in PHP as it limits the scope of variables, reducing the chance of variable name collisions. It also makes the code easier to understand and debug, as you can easily see where each variable is defined and where it can be accessed.
Troels has been crafting web applications, as a freelancer and while employed by companies of various sizes, since around the time of the IT-bubble burst. Nowadays, he's working on backend systems for a Danish ISP. In his spare time, he develops and maintains Konstrukt, a web application framework for PHP, and is the organizer of a monthly PHP-meetup in Copenhagen.