Simple Debugging with func_get_args()

When working with a friend of mine, he kept asking me the arguments for functions, and when I told him to try api.drupal.org or my favorite drupalfunctions.com he told me that sometimes it is not the case and what he wanted to know was what was really being past to the function.

Well I told him that was quite, simple just dpm() the arguments of the function. Looking at me bewilder, I showed him the function func_get_args() and how when it is combines with dpm() it will show you exactly what is called.

so in a simple hook like hook_node_view() you can find out what is really being passed.

/**
 * Implements hook_node_view().
 */
function foo_node_view() {
  dpm(func_get_args());
}

doing this will always tell you what is happening.

Another thing I also showed him. was the __FUNCTION__ which gives you a string of the current function. so adding

dpm(__FUNCTION__);

to any function will tell you when the function is called, and doing this means that you can cut and past this into any function without any changes. Makes for easy debugging. Also you can try __FILE__ or __LINE__ and you can fine more magic contants at http://php.net/manual/en/language.constants.predefined.php

Tags: 

Comments

func_get_args() normally

func_get_args() normally doesn't work inside another function call. If it is, php's being gracious to you; usually I see a fatal error.

// To get around any errors
$args = func_get_args();
dpm($args, 'arguments');

Anyway, this is a VERY handy way to debug, because you can see if additional arguments are being passed into functions from other modules as well.