views:

71

answers:

2

I need a way to see all the defined variables of the current PHP instance.
By currently defined I mean all the globals, all the local to THIS scope and all the locals to other scopes.
Is there something built in?

+6  A: 

For everything in the current scope:

print_r(get_defined_vars());

I don't think there is a solution to dump all variables from all scopes, because you would have to actually run those functions/methods to get a complete and intact map of all available variables (variables could get created, added and removed at runtime).

You may have to look into Unit Testing or code coverage tools to see whether you can use anything from those areas, but it's going to be a lot of work.

What do you need this for?

Pekka
I am dealing with a huge project (I mean, really, hundred of thousands of lines of code). I some times need to see the "bigger" picture of what is happening.
Itay Moav
@Itay I see. Did you inherit the project? Is the code documented at all? That kind of overview is best achieved by in-line documentation that phpdoc can make sense of.
Pekka
@pekka - you are right in all your assumptions, except the ones about documentation :-DDD (i.e. there is no documentation, no meaningful enough symbol names etc)
Itay Moav
@Itay I see, *that* kind of project! :D
Pekka
Switch to Java.
@Walter White - You do know that 100's of thousands of lines in PHP translate to millions in java ;-)
Itay Moav
If you really want to believe that.As for the root problem, perhaps you can use dtrace if you're on a Solaris platform.
On the PHP projects I've worked on, I tend to notice more copy and paste as well as fewer design patterns being followed. I agree, on the Java side, development tends to follow patterns more so things may become more complex, but not necessarily more code even if you include the amount of code in the frameworks.PHP is a scripting language, which lends itself to developing a simple solution to a problem quickly. Problem is, while you can develop more quickly because you're less strict, those to do items never get done. So, you're often left with copy and paste and other anti-patterns.
@Walter I see where you're coming from, but even though PHP is a very permissive language, it *is* possible to develop professional software with it. PHP has a very low barrier of entry, so the proportion of crappy software vs. high-quality software is higher than in a strict language like Java - still, it is possible for a decent developer to build a decent product in PHP, particularly since PHP 5.3.
Pekka
@Pekka, I agree with your comment, the barrier is much lower.In any case, is it possible for him to use DTrace? He has to be on Solaris to use that tool.
@Walter I don't know DTrace but I doubt it will helpful, seeing as each function would actually have to be executed - and executed correctly - for all variables to be initialized reliably. I think this can only be done using code that has been optimized for Unit Testing and Code Coverage checking purposes - I can't see any other way how to do this. (Except of course for documenting all variables you spot in the functions. But the fact that you don't need to declare a vairable, but can conjure it out of thin air like `$new_var = "Hello!"` in PHP can make that a tedious task.)
Pekka
A: 

var_dump($GLOBALS);

If a variable is not defined at the time this statement runs, then it is impossible to talk about it as having a value. Variables in "other" scopes don't exist.

{
    $a = new myClass();
    do stuff
    $a->destroy();
}
print "$a has no meaning in this context";
Full Decent
What do you mean "does not exists" they still take memory, right?
Itay Moav
I think I see, think about function call inside a function call inside a function call, and I have to know it inside the inner most function.
Itay Moav