Suppose say, you want to debug variables in some observer method.
You can either log each variable or simply dump it. But when you have many variables, debugging each variable can be an overhead task.
For this purpose we can use PHP’s inbuilt function: get_defined_vars()
How to use it?
//put this line at the top of your variable declarations
$vars = get_defined_vars();
// Now your regular stuffs goes here...
$foo = 'foo';
$bar = 'bar';
$data = $model->getData();
// Only stores all the variables defined in current scope
$vars = array_diff(get_defined_vars(), $vars);
//Now you can either dump
Zend_Debug::dump($vars);
//or keep in log
Mage::log($vars, null, 'var-debug.log', true)
This approach can be very handy in debugging any local scope variables.
At least it provides an easier approach with clean code.
Why dumping when XDEBUG exists and lets you do a lot of powerful and fun stuff? And saves you a lot of time because it lets you navigate the stack and shows you the state of the involved variables in each step of the stack.
Yes you are absolutely correct. But this article only shows the usage of get_defined_vars() when you don’t have xdebug or so.