Using PHP 7.0, consider the code below:
<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
$closure();
}
To me, the expected output would be:
PHP Notice: Undefined variable: closure
But somehow the result is
B
Then consider this code below:
<?php
namespace A {
$closure = function() {
echo __NAMESPACE__;
};
}
namespace B {
$closure = function () {
echo __NAMESPACE__;
};
}
namespace C {
\A\$closure();
}
Now knowing (but not yet understanding) the behavior of the first example the expected output to me would be:
A
But instead I get
PHP Parse error: syntax error, unexpected '$closure' (T_VARIABLE), expecting identifier (T_STRING)
This behavior completely confuses me.
Question part 1: can someone explain me what is wrong with my expectation in the first example? Question part 2: how is the behavior consistent with the first example?