accessing local variables, without passing them?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sarke
Forum Newbie
Posts: 11
Joined: Thu Feb 15, 2007 12:12 am

accessing local variables, without passing them?

Post by Sarke »

Ok, so what I have is something like this...

Code: Select all

<?php

function numbers()
{
	$a = 1;
	$b = 1;
	$c = 1;
	$d = 1;
	$e = 1;
	$f = 1;

	$sum = 0;

	add();

	echo $sum;
}

function add()
{
	global $a, $b, $c, $d, $e, $f, $sum;

	$sum = $a + $b + $c + $d + $e + $f;
}

numbers();

?>
The above code doesn't work of course, but I hope it gives you an idea of what I want it to do. I want to be able to access the local variables in numbers() without having to pass it as a function paramater. From add() I can access the global scope, and of course it's own local scope, but what about the scope numbers()? It is being called from there, so I'm hoping there is a way. Is there a way, without making the local variables in numbers() into globals or passing them as parameters?



EDIT: or alternativly, is there a way to pass them all in a single array without having to type them all out? What I don't want to have to do is this:

Code: Select all

<?php

function numbers()
{
	$a = 1;
	$b = 1;
	$c = 1;
	$d = 1;
	$e = 1;
	$f = 1;

	$sum = 0;

	add(array('a' => &$a, 'b' => &$b, 'c' => &$c, 'd' => &$d, 'e' => &$e, 'f' => &$f, 'sum' => &$sum));

	echo $sum;
}

function add($locals)
{
	$locals['sum'] = $locals['a'] + $locals['b'] + $locals['c'] + $locals['d'] + $locals['e'] + $locals['f'];
}

numbers();

?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You could send your numbers as an array to the add function and have it loop through them and add, then return the sum.

Code: Select all

function numbers()
{
   $numbers = array($a,$b,$c,$d,$e,$f);
   echo add($numbers);
}

function add($numbers)
{
   $sum = 0;
   foreach($numbers AS $number)
   {
      $sum += $number;
   }

   return $sum;
}

numbers();
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

You can "collect" all locally scoped variables using get_defined_vars():

Code: Select all

function numbers() {
	$a = 1;
	$b = 99;
	return add(get_defined_vars());
}

function add($vars) {
	extract($vars);
	return $a + $b;
}


echo numbers();
Sarke
Forum Newbie
Posts: 11
Joined: Thu Feb 15, 2007 12:12 am

Post by Sarke »

Thanks for the replies guys.


scottayy, I don't want to have to type out all the variables. The above was just an example, but there are many places where I need to grab the local variables and having to track them all down is hard work (and possibly missing some). I also want to be able to modify several of them (just like if they were globals), so having a return value doesn't work well.


stereofrog, that also requires me to have a return value. But get_defined_vars() did point me in the right direction, thanks. I found that this works closer to what I need:

Code: Select all

<?php

function numbers()
{
	$a = 1;
	$b = 1;
	$c = 1;

	foreach (array_keys(get_defined_vars()) as $var)
		$locals[$var] = &$$var;

	add($locals);

	echo $a;
	echo $b;
	echo $c;
}

function add($locals)
{
	$locals['a']++;
	$locals['b']++;
	$locals['c']++;
}

numbers();

?>

It's a bit hard t oexplain, but I want to call a function in several functions that executes some code that may or may not change any or all of the local variables in the function it was called from.



P.S. it's not everyday you get to use &$$ :P
Post Reply