Page 1 of 1

accessing local variables, without passing them?

Posted: Thu Mar 29, 2007 2:48 am
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();

?>

Posted: Thu Mar 29, 2007 3:14 am
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();

Posted: Thu Mar 29, 2007 3:44 am
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();

Posted: Thu Mar 29, 2007 5:33 am
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