Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Debug vanilla

Parent Feed: 

Most of the time, when working on some piece of code, I'll resort to the configured debugger in my current Zend Studio configuration. And you probably do too :-)

However, I often have to access debug-type information on live sites where installing a debugger is out of the question, and I find myself often resorting to parameter dumps like the following:

<?php
// lazy version for simple cases
function foo_bar($x, $y, $z) {
 
dsm(func_get_args());
 
// [...]// less lazy version for more hairy cases
function foo_baz($x, $y, $z) {
 
dsm(array('in foo_baz, x' => $x, 'y' => $y, 'z' => $z));
 
// ...
?>

You've probably being using it too and, of course, after the first few dozen times, it becomes a bit used. So here's a tiny snippet that makes such dumps simpler to type and use :

<?php
function foo_quux($x, $y, $z) {
 
dsm(func_get_args_hash());
 
// [...]/**
* Provide a better func_get_args()
*
* @return array
*   Hash of parameters, keyed by parameter name
*/
function func_get_args_hash() {
 
$stack = debug_backtrace();
 
$stack = $stack[1];
 
$rf = new ReflectionFunction($stack['function']);
 
$params = $rf->getParameters();
 
$ret = array();
  foreach (
$params as $index => $param) {
   
$ret[$param->name] = $stack['args'][$index];
  }
  return
$ret;
}
?>
Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web