Slips Posted December 28, 2010 Share Posted December 28, 2010 Hello all, I have some piece of code that is nested like this $variable = 'This is a global argument'; function parentFunction($variable) { function childFunction() { echo 'Argument of the parent function is '.$GLOBALS['variable']; } childFunction(); } parentFunction(5); What I want to know is - Is there a way to access a variable from the parent function without passing arguments to the child function? (Something like how classes have parent::?). I don't want to use $GLOBALS because it might cause some variable collision, and I didn't want to pass arguments because incase I decide to change the arguments in the parent function I'd have to do it in the child function aswell. From my searching around in the Internet it seems like this is not possible, but if theres a slight chance that there might be something out there, i'm willing to give it a shot . Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/222807-accessing-a-variable-inside-the-parent-function-from-a-child-function/ Share on other sites More sharing options...
trq Posted December 28, 2010 Share Posted December 28, 2010 It is not possible without using the methods you have described. Why are you nesting functions in the first place? I'm not sure that is a language feature myself. Quote Link to comment https://forums.phpfreaks.com/topic/222807-accessing-a-variable-inside-the-parent-function-from-a-child-function/#findComment-1152078 Share on other sites More sharing options...
Slips Posted December 28, 2010 Author Share Posted December 28, 2010 Ah damn, but anyways I was doing it this way because the child function was needed to repeat some actions in the parent function code and it seemed like the only way I know, from my knowledge(and it isn't a lot lol). My other choice was to use goto: but I've read a bit about how goto can create some bad spaghetti code.. Quote Link to comment https://forums.phpfreaks.com/topic/222807-accessing-a-variable-inside-the-parent-function-from-a-child-function/#findComment-1152079 Share on other sites More sharing options...
kenrbnsn Posted December 28, 2010 Share Posted December 28, 2010 Just define the child function with a parameter and pass it in as usual. <?php function parentFunction($var) { childFunction($var); } function childFunction($cvar) { echo "Argument of the parent function is $cvar"; } parentFunction(5); ?> Also, when you do it your way, you will get an error, if you call the parrentFunction more than once unless you wrap the childFunction's definition in an <?php if (function_exists('childFunction')) { ?> if block. Ken Quote Link to comment https://forums.phpfreaks.com/topic/222807-accessing-a-variable-inside-the-parent-function-from-a-child-function/#findComment-1152099 Share on other sites More sharing options...
Slips Posted December 28, 2010 Author Share Posted December 28, 2010 Yep, I finally settled for passing arguments. And i did use if (function_exists... The main reason I wanted to do it that way was to make sure there is as less dependent code as possible (i.e have to change the arguments if the parent arguments change in future, and the code was using around 5-7 arguments) Quote Link to comment https://forums.phpfreaks.com/topic/222807-accessing-a-variable-inside-the-parent-function-from-a-child-function/#findComment-1152220 Share on other sites More sharing options...
kenrbnsn Posted December 28, 2010 Share Posted December 28, 2010 If you're concerned about having to change the number of arguments passed, use an array to pass the arguments. That way you're always passing one argument. How the values within the array are handled is up to the coding. Ken Quote Link to comment https://forums.phpfreaks.com/topic/222807-accessing-a-variable-inside-the-parent-function-from-a-child-function/#findComment-1152293 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.