note 104908 deleted from function.func-get-args by tularis
| From: | tularis@php.net | Date: | Fri, 15 Jul 2011 09:01:42 +0000 |
| Subject: | note 104908 deleted from function.func-get-args by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181556@lists.php.net to get a copy of this message | ||
Note Submitter: daevid at daevid dot com
----
I slightly improved version of the previous poster's awesome function.
<?php
/**
* Useful for debugging functions to see parameter names, etc.
* Based upon http://www.php.net/manual/en/function.func-get-args.php#103296
*
* function anyfunc($arg1, $arg2, $arg3)
* {
* debug_func(__FUNCTION__, '$arg1, $arg2, $arg3', func_get_args());
* //do useful non-debugging stuff
* }
*
* @access public
* @return string
* @param string $function_name __FUNCTION__ of the root function as passed into this function
* @param string $arg_names the ',,,' encapsulated parameter list of the root function
* @param array $arg_vals the result of func_get_args() from the root function passed into this
function
* @param boolean $show_empty_params (FALSE)
* @author Daevid Vincent
* @date 2011-17-14
* @see func_get_args(), func_get_arg(), func_num_args()
*/
function debug_func($function_name, $arg_names, $arg_vals, $show_empty_params=true)
{
$params = array();
echo $function_name.'(';
$arg_names_array = explode(',', $arg_names);
//var_dump($arg_names, $arg_names_array, $arg_vals );
foreach($arg_names_array as $k => $parameter_name)
{
$v = $arg_vals[$k];
//echo "k = $parameter_name = $k and v = arg_vals[k] = $v<br>\n";
//var_dump($v);
//[dv] I discovered a bug in PHP 5.3.6 where is_string() and is_null() are reversed in case
statements?!
// Therefore I had to split these off to if/else until it's fixed. :-\
if (is_string($v))
{
if ($show_empty_params) $v = "'".$v."'";
}
elseif (is_null($v))
{
if ($show_empty_params) $v = '{NULL}';
}
else
switch($v)
{
//case is_string($v): if ($show_empty_params) $v = "'".$v."'"
; break;
//case is_null($v): if ($show_empty_params) $v = '{NULL}'; break;
case is_bool($v): $v = ($v) ? '{TRUE}' : '{FALSE}'; break;
case is_array($v): (count($v) < 10) ? $v =
'['.implode(',',$v).']' : $v = '[ARRAY]'; break;
case is_object($v): $v = '{CLASS::'.get_class($v).'}'; break;
case is_numeric($v): break;
}
if ($show_empty_params || $v) $params[$k] = trim($parameter_name).': '.$v;
//echo "params[$k] = ".$params[$k]."<br>\n";
}
echo implode(', ',$params).")<br/>\n";
}
?>