nested - Nesting PHP-functions: to what purpose? -
why php allow nesting functions?
<?php function foo() { function bar() { return "bar"; } return "foo"; } print foo(); print bar();
.. valid php.
but:
- why nesting needed ever?
- and if so, why can call bar anywhere (and not, e.g. withing foo(), or trough foo.bar() or such).
i ran today, because forgot closing bracket somewhere, , had 1 many further down. code valid , no errors thrown; started acting weird. functions not being declared, callbacks going berserk , on. feature, , if so, purpose? or idiosyncrasy?
answer: commentor points out duplicate of what php nested functions for.
note order important here; cannot call bar() before calling foo() in example. logic here appears execution of foo() defines bar() , puts in global scope, it's not defined prior execution of foo(), because of scoping.
the use here primitive form of function overloading; can have bar() function perform different operations depending upon version of foo() declares it, assuming of course each different version of foo() indeed defines bar() function.
Comments
Post a Comment