I was wondering why php handles the scope of a declared function within a function differently when a function is declared inside a class function.
For example:
function test() // global function
{
function myTest() // global function. Why?
{
print( "Hello world" );
}
}
class CMyTestClass
{
public function test() // method of CMyTestClass
{
function myTest() // This declaration will be global! Why?
{
print( "Hello world" );
}
}
}
}
Can anybody explain this to me why this happen? Thank you for your answer.
Greetz.
In PHP all functions are always global, no matter how or when you define them. (Anonymous functions are partially an exception to this.) Both your function definitions will thus be global.
From the documentation:
Now all php functions are global by default. So your nested function becomes global the second you call the outer function