Why would PHP allow nesting functions?
<?php
function foo() {
function bar() {
return "bar";
}
return "foo";
}
print foo();
print bar();
.. is valid PHP.
But:
- Why would nesting be needed ever?
- And even if so, why can I call bar from anywhere (and not, e.g. only withing foo(), or trough foo.bar() or such).
I ran into this today, because I forgot a closing bracket somewhere, and had one too many further down. The code was valid and no errors thrown; but it all started acting really weird. Functions not being declared, callbacks going berserk and so on. Is this a feature, and if so, to what purpose? Or some idiosyncrasy?
ANSWER: commentor points out that this is a duplicate of What are php nested functions for.