blueman378 Posted December 9, 2008 Share Posted December 9, 2008 Hi guys. I was looking into a way to tidy up my code and i came across the idea of nested functions: a quick google found this: When you define a function within another function it does not exist until the parent function is executed. Once the parent function has been executed, the nested function is defined and as with any function, accessible from anywhere within the current document. If you have nested functions in your code, you can only execute the outer function once. Repeated calls will try to redeclare the inner functions, which will generate an error. why would you need nested functions? any examples of them being used? Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/ Share on other sites More sharing options...
trq Posted December 9, 2008 Share Posted December 9, 2008 I've never found any need for them. Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710308 Share on other sites More sharing options...
blueman378 Posted December 9, 2008 Author Share Posted December 9, 2008 so are nested functions something thats coded into php to support them, or is it just something that was there by way of other pieces that they just left? Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710312 Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 It's kind of a primitive way of procedural encapsulation I guess... I've certainly never found a use for it. I'm sure they put it in, it's doubtful it was just a byproduct of the compiler, other programming languages have them as well. Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710318 Share on other sites More sharing options...
PravinS Posted December 9, 2008 Share Posted December 9, 2008 I have used nested function to generate multilevel dynamic menu. Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710319 Share on other sites More sharing options...
blueman378 Posted December 9, 2008 Author Share Posted December 9, 2008 I have used nested function to generate multilevel dynamic menu. could i see an example code? is it sort of a case as each level is a different function if the upper levels dont get declared then the lower ones dont need to exist? Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710321 Share on other sites More sharing options...
redarrow Posted December 9, 2008 Share Posted December 9, 2008 It seems that one advantage is that you could declare the same function in different contexts. Consider a dynamic function call: $functionname="functionone"; Now if you want, you can call: $functioname(); myecho(); let us say you have defined <?php function functionone(){ ..function myecho(){ ....echo "I am one"; ..} } function functiontwo(){ ..function myecho(){ ....echo "I am two"; ..} }` ?> Now you can call myecho(), and you'll get "i am one". Change the original $functionname to "functiontwo" and the same myecho() call will now spit out "I am two". Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710359 Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 Yeah, if only I could find a use for that procedural polymorphism? no thanks, I'll stick with my objects. Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710366 Share on other sites More sharing options...
blueman378 Posted December 9, 2008 Author Share Posted December 9, 2008 lol so basically its just another case of we can put it in so we will? Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710372 Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 Well other people put it in, SOMEONE must swear by them somewhere, I just don't know who the heck the guy is. Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710373 Share on other sites More sharing options...
Yesideez Posted December 9, 2008 Share Posted December 9, 2008 Freaky! In almost 30 years of programming I've never had the need to do anything like this and never knew this existed! Quote Link to comment https://forums.phpfreaks.com/topic/136176-nested-functions-why/#findComment-710376 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.