Jump to content

acessing outher function variable from variable function


Goat

Recommended Posts

It is a bit hard to explain so I'll just show an example:


function foo()
{
   $a = "changed by variable function";
}

function bar($var_function)
{
   $a = "original value";
   $var_function();
   echo($a); 
}

bar(foo); // outputs "original value", not "changed by variable function"

 

I can access global scope using global keyword, but there does not seem to be a way to access local function variables. Any ideas?

 

regards,

 

Goat

 

Link to comment
Share on other sites

You would need to pass a reference of $a. This way instead of actually passing the value of $a you're passing the location in memory of $a.

 

function foo(&$a)
{
   $a = "changed by variable function";
}

function bar($var_function)
{
   $a = "original value";
   $var_function($a);
   echo($a); 
}

echo bar('foo');

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.