I'm trying to visualize how javascript and php treat nested function.
The point is this:
php:
b(); //CANNOT call b at this point because isn't defined yet
a(); //CAN call a at this point because the interpreter see the declar
b(); //ok now i can call b, because the interpreter see the declaration after a execution
function a(){
function b(){
echo "inner";
}
}
meanwhile in javascript:
b(); //CANNOT call b isn't defined yet
a(); //CAN call a at this point because the interpreter see the declar
function a(){
function b(){
console.log("inner");
}
}
a(); //ok now i can call a because is defined
b(); // **CANNOT** call b yet !!
why in javascript i can't call b() even if a is executed? in what PHP act different?
Thank's in advance!
1 Answer 1
Its a scope thing. You could have as easily - in javascript - written "var b = function()". "b" is just a variable of type function defined within the scope of the function a. In PHP, both "a" and "b" are global functions, but it's the job of function "a" to define "b", so it won't get defined until "a" is called. Consider this example...
function a($x) {
if ($x) {
function b() { echo "x not empty"; }
} else {
function b() { echo "x empty"; }
}
}
a(1); // Defines function b
b(); // x not empty
a(0); // PHP Fatal error: Cannot redeclare b() (previously declared...
You can see by the failure to redefine "b", that "b" is a real, globally scoped function. Function "a" could use various criteria to define the function for a particular purpose in different runs. Clearly, in this case, it wouldn't make sense to call function "b" before function "a" has decided how to define it.
I don't, by the way, think the example above is very good coding practice, but it does serve to illustrate the point.
The PHP code most similar to your javascript code would be:
function a() {
$b = function() {
echo "'b' says inner";
};
$b(); // Demonstrating the function can be used inside "a"
}
a(); // 'b' says inner
$b is a variable of type function, which can only be used within function "a".
-
Ok thank's! the point of my question was the difference about scoping of the inners function and the role of interpreter in this! Is more clear now!artas– artas2019年06月25日 08:18:30 +00:00Commented Jun 25, 2019 at 8:18
-
@artas Why languages are what they are tends to become a bit of an existential discussion. You can understand javascript a bit better if you get back to its roots. Google "syntactic sugar and javascript" and you find a lot of modern javascript is just that. Without it javascript becomes less fun to write and harder to read, but you do get a deeper understanding of what it's doing. PHP is, well, PHP - no particular rhyme or reason to why its needle in a haystack in one function and haystack with a needle in another. You just have to learn it all...wordragon– wordragon2019年07月04日 14:59:26 +00:00Commented Jul 4, 2019 at 14:59
-
thank's for you answer! yes I know that but my question isn't about "why" but about "how" the scoping are different. in particular how php and javascript differ in treat definition of function inside another function!artas– artas2019年07月04日 16:41:22 +00:00Commented Jul 4, 2019 at 16:41
Explore related questions
See similar questions with these tags.
let
andconst
, that also applies to simple blocks.