PHP: query all variables defined inside function scope - php

This is just out of curiosity, but I was wondering if there was a way to query all the variables defined inside the scope of a function (exclusively within this scope, and from within that function) and put them in an associative array. Something like an extended get_defined_vars function.
The reason is that it would be nice to be able to save the 'state' of an execution at any point in the program, for instance to debug, log, handle exceptions, or even pass the entire scope of a function to another one. If I'm not mistaken, I think get_object_vars allows doing this with objects.

From the comments of PHP.net
// The very top of your php script
$vars = get_defined_vars();
// Now do your stuff
$foo = 'foo';
$bar = 'bar';
// Get all the variables defined in current scope
$vars = array_diff(get_defined_vars(),$vars);
echo '<pre>';
print_r($vars);
echo '</pre>';

PHP doesn't really have scopes, they were added later, that's why you need $this and global to access a global variable. Objects are implemented basically as arrays because they came later, that's why equality tests on two objects work member by member.
You can try var_dump of course.
Ignore the var_dump bit, you've found get_defined_vars and get_object_vars they are as close as you will get I'm sorry to say.
Another difficulty in telling you about what's in a function is how it does variables, remember PHP is a dynamic language if we have:
$x = "hello";
$$x = "world"
we now have a variable called $hello with value "world", if the $x variable came from some complicated nondeterministic code, it'd be impossible to tell without running. So you can only inspect the current variables, when you're in it, and given how scopes are implemented, get_defined_vars is as close as you will get sorry.

Related

Access local variable from other function PHP

I have a private function which has lots of variables, let's say these: $foo, $bar, $baz.
Inside the function, I call parent::_setViewVars(array('foo','bar','baz'));
This _setViewVars looks so:
protected function _setViewVars($a){
foreach($a as $v){
global $$v;
$this->set($v, $$v);
}
}
I just want to get rid off cakephp's shit like 80 lines of $this->set('selected_areas',$selected_areas);, that's what this function is for.
_setViewVars cannot access $$v, because it's not a global variable. Given that this function is being called from the context that has these variables, can I reach them somehow?
(by the way if there is a cakephp way of bulk setting view variables with the same names, tell me how -- I haven't found one)
PS: packing everything into an array is NOT a solution, we're talking about thousands of unneccessary square brackets here.
What you're looking for is actually pretty standard PHP:
compact() - Create array containing variables and their values
Example showing it's use in a CakePHP Action:
$var1 = "I";
$var2 = "love";
$var3 = "pizza!";
$this->set(compact('var1', 'var2', 'var3'));
(might want to try to tone down the hatred a bit - you're more likely to get help that way)

Variable variable string constructed for "$GLOBALS" works within global scope, but not function scope

An important note: $GLOBALS are dirty and evil. Don't use them. Ever. Never ever ever.
Please focus on the fact that it doesn't work and not why you would be doing this in the first place, it is purely a theoretical question about a technical exercise.
This is a rather weird one. I'm attempting to construct a variable variable using a string named $GLOBALS.
From the global scope
Let's see what we get when var_dump()ing this in the global scope.
$g = sprintf('%s%s%s%s%s%s%s', chr(71), chr(76), chr(79), chr(66), chr(65), chr(76), chr(83));
var_dump($$g);
The result is an array of global variables, which you can see here. Great! So, let's try this in a function.
From a function scope
First, let's just make sure that we can actually run an $GLOBALS check within a function.
function globalAllTheThings()
{
var_dump($GLOBALS);
}
globalAllTheThings();
The result is: it works!! You can see this here.
Now, let's try the first test that we used in the global scope, within the function, and see what happens.
function globalAllTheThings()
{
$g = sprintf('%s%s%s%s%s%s%s', chr(71), chr(76), chr(79), chr(66), chr(65), chr(76), chr(83));
var_dump($$g);
}
globalAllTheThings();
For simplicity's sake
You can also try this without the weird obfuscation (don't ask).
function globalAllTheThings()
{
$g = 'GLOBALS';
var_dump($$g);
}
globalAllTheThings();
It returns NULL. What's that about?? Why does it return NULL, and what can I do to get this working. Why, you ask? For educational purposes of course, and for science!
Because the manual says so:
Warning
Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.
http://php.net/manual/en/language.variables.variable.php
It's simply "special". PHP is "special". Superglobals don't play by the same rules as regular variables to begin with. Someone forgot to or decided against making them compatible with variable variables in functions. Period.

Help me understand PHP variable references and scope

References:
If I pass a variable to a function (e.g. $var), is that supposed to be a copy of a reference to the actual variable (such that setting it null doesn't affect other copies)?
Or is it receiving a reference to what is a new copy of the actual variable (such that setting it to null destroys its copy only)?
If the latter, does this copy objects and arrays in memory? That seems like a good way to waste memory and CPU time, if so.
I think can understand passing by reference (e.g. &$var) correctly by knowing how this works, first.
Scope:
What's the deal with local scope? Am I right in observing that I can declare an array in one function and then use that array in other functions called within that function WITHOUT passing it to them as a parameter?
Similarly, does declaring in array in a function called within a function allow it to be available in the caller?
If not, does scoping work by a call stack or whatever like every bloody thing I've come to understand about programming tells me it should?
PHP is so much fun. :(
If I pass a variable to a function (e.g. $var), is that supposed to be a copy of a reference to the actual variable (such that setting it null doesn't affect other copies)?
Depends on the function. And also how you call it. Look at this example:
http://www.ideone.com/LueFc
Or is it receiving a reference to what is a new copy of the actual variable (such that setting it to null destroys its copy only)?
Again depends on the function
If the latter, does this copy objects and arrays in memory? That seems like a good way to waste memory and CPU time, if so.
Its going to save memory to use a reference, certainly. In php>4 it always uses reference for objects unless you specify otherwise.
What's the deal with local scope? Am I right in observing that I can declare an array in one function and then use that array in other functions called within that function WITHOUT passing it to them as a parameter?
No you can't.
Similarly, does declaring in array in a function called within a function allow it to be available in the caller?
No, it doesn't.
If not, does scoping work by a call stack or whatever like every bloody thing I've come to understand about programming tells me it should?
If you want to use a variable from outside the function, before using it, you'd write global $outsidevar
Concerning your first set of questions:
foo($a);
function foo($b) { echo $b; }
In this case, $a will not be copied to a new variable $b, only because it is passed by value.
This is because PHP uses the copy-on-write concept. PHP will not copy the contents of a variable, unless they are changed. Instead PHP will increment the refcount property of the existing "zval" of $a.
Well, the whole thing is not that trivial, but to answer your question: No, it does not copy the variable, unless you write to it in the function and no, you won't save CPU and Memory by using a reference. In most cases the reference won't change performance at all, but in the worst case it will actually degrade it (because if a not is_ref variant of the variable already exists and a reference is created the value of the variable must be copied to get a zval with is_ref and one without). Optimizing code by using references is no good.
if argument to a function is defined as so "function my_function($variable) {}" then you are getting a copy of the variable and any alterations made to the variable inside your function will not be available to the function caller. you can pass a variable by reference by prepending an ampersand to the argument when defining your function and thus any alterations made to the variable will persist to the function caller, ie "function my_function(&$variable) {}"
function myfunction($var) {
$var = 'World';
}
$var = 'Hello';
myfunction($var);
echo $var; // 'Hello';
Passing a variable by reference
function myfunction(&$var) {
$var = 'World';
}
$var = 'Hello';
myfunction($var);
echo $var; // 'World'

Function to create new, previously undefined variable

Sorry for such a lame title, but I just had no idea what to put there, hope you understand it. Plus, I have no idea if similar question has been asked before, because I don't know the proper keywords for it - therefore couldn't google it too.
Basicly... When looking at preg_match_all();, they got this matches parameter that will create new array defined in function, and give us the ability to access it after function execution.
And the question is.. How can I implement such a feature in my own function? But that it could create single variable and/or array.
Thanks in advance!
preg_match_all() accepts a reference to an array, which in its own scope is called $matches. As seen in the function prototype:
array &$matches
If you call the function and pass in a variable, if it does not already exist in the calling scope it will be created. So in your user-defined function, you accept a parameter by reference using &, then work with it inside your function. Create your outer-scope variable by simply declaring it in your function call, like you the way you call preg_match_all() with $matches.
An example:
function foo(&$bar) {
$bar = 'baz';
}
// Declare a variable and pass it to foo()
foo($variable);
echo $variable; // baz
I think you are referring to function parameters passed by reference, are you not?
function putValInVar(&$myVar, $myVal){
$myVar = $myVal;
}
$myVar = 1;
putValInVar($myVar, 2);
echo $myVar; // outputs '2', but will output '1' if we remove the '&' //
By default function arguments in PHP are passed by value. This means that new variables are created at each function call and those variables will exist only inside the function, not affecting anything outside it.
To specify that an argument should be used by reference the syntax is to append an & before declaring it in the function header. This will instruct PHP to use the passed variable inside the function rather than creating a copy of it.
Exception: Objects are always passed by reference. (Well... Not really, but it's complicated. See the comment thread for more info.)
I think what you are asking for is passing-by-reference. What preg_match_all basically does to "create" an array variable outside its scope is:
function preg_match_all($foo, $bar, & $new_var) {
$new_var = array(1,2,3);
}
The crucial point here is & in the function definition. This allows you to overwrite variables in the outer scope when passed.
Stylistically this should be used with care. Try to return arrays or results instead of doing it via reference passing.
Like this:
$myvariable = runfunction();
function runfunction() {
//do some code assign result to variable (ie $result)
return $result;
}
Or
global $result;
function runfunction() {
global $result;
$result = 'something';
}

What's the benefit of function refrence?

When I reading the code of CodeIgniter,I found some functions written as follows:
function &get_instance()
{
global $CI, $OBJ;
if (is_object($CI))
{
return $CI;
}
return $OBJ->load;
}
I can understand variable refrence,but I can hardly get this through.Is it necessary to use this function style?And any benefit?
Thanks.
In PHP's syntax, this means that the function returns a reference instead of a value. For example:
<?php
$foo = 'foo';
function & get_foo_ref ()
{
global $foo;
return $foo;
}
// Get the reference to variable $foo stored into $bar
$bar = & get_foo_ref();
$bar = 'bar';
echo $foo; // Outputs 'bar', since $bar references to $foo.
?>
In the above example, removing the & from the function decleration would make the $foo variable still contain 'foo', since only the value, not the reference was returned from the function.
This was used more often in PHP4, because it did not pass objects by their reference and cloned them instead. Because of this, object variables had to be passed by reference to avoid unwanted cloning. This is no longer the case in PHP5 and references should not be used for this purpose.
However, functions that return references are not completely useless either (or bad practice, when not used for replacing object references). For example, personally I've used them when creating a script that passes a "path" to an function, which returns reference to variable in that path allowing me to set value to it and read the value. Due to recursive nature of the function, returning the reference was needed.
It is the same concept as variable reference, in fact this "style" is required when you want to return a REFERENCE to a variable and not the value itself.
Explained in the manual here: http://www.php.net/manual/en/functions.returning-values.php
no, there's no benefit. It was necessary for php4, but in php5 it's a bad practice.

Categories