Jump to content

Accessing a variable inside the parent function from a child function


Slips

Recommended Posts

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 :D.

 

Thanks in advance

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.