3

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.

2 Answers 2

1

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>';
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks :) I think this does the job, but I'll wait to see if there's a better answer before marking this as the solution
0

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.

2 Comments

Thanks for your quick answer. Maybe the answer is somewhere else; do you know of any IDE supporting step-by-step execution/breakpoints in PHP? If so, they must have a tool to analyze PHP code locally, no?
Actually PHP is just an interpreter, there's a program called "php" that'll run it for you much like "python" would, Eclipse is a good IDE that allows you to debug it also supports autocompletion, and is able to use type-hinting (BUT I DON'T RECOMMEND YOU USE TYPE-HINTING) on a Debian based computer try sudo apt-get install php but be warned, you may have to install modules like MySQLi and stuff. It can definitely done though, even if it is a bit more complicated than Python debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.