In need of better __FILE__, __LINE__ and __FUNCTION__ magic variables
am 26.01.2010 04:36:38 von Daevid VincentLike you, I have many little functions that are useful for debugging in a
page.
The problem is that when you start to pepper them around, whilst debugging,
you can often times forget where you left them. A search isn't always
helpful since I sometimes leave them in the code, but commented out, so I
end up finding dozens of matches.
What I need are some variables that tell me the current calling file, line
and function I'm in. Then when I see some debug info on the screen, I know
exactly where it came from.
http://www.php.net/manual/en/language.constants.predefined.p hp are for the
most part useless for this task, as they tell you information about the
file, line and function name itself. This is a subtle but significant
difference.
Say I have a typical setup:
common.inc.php -- has all my helper routines and fancy debug functions.
mywebpage.inc.php -- the functions for mywebpage as I follow MVC logic.
mywebpage.php -- the page I'm trying to work on and debug.
If I place a print_x($foo) in mywebpage.inc.php somewhere, lets say in a
function insert_into_database() on line 69, I would expect that __FILE__,
__LINE__ and __FUNCTION__ to reflect all of that. It should return with
"mywebpage.inc.php", "69" and "insert_into_database" respectively.
What in fact happens is that I get "common.inc.php", "642", "print_x". If I
use $_SERVER['PHP_SELF'] instead of __FILE__ then I get "mywebpage.php"
(not "mywebpage.inc.php" as desired)
Now, the __FILE__, __LINE__ and __FUNCTION__ have their merit in some
cases, but I think there needs to be another set of magic variables that
are more dynamic and work as I suggest. Perhaps a __CFILE__, __CLINE__ and
__CFUNCTION__ with the "C" prefix for "Current" or "Calling"
/**
* Print out a pretty, portlet, styled version of an array
*
* @param array $array Array to print out
* @param boolean $var_dump use either print_r (false/default) or
var_dump
* @param string $string if set, this will print out in the title of
the debug portlet
*/
function print_x($array, $var_dump=false, $string='')
{
if (!is_array($array))
{
echo ''.$_SERVER['PHP_SELF'].'::'.__FUNCTION__."(Not An
Array) $string
\n";
return;
}
?>
= $_SERVER['PHP_SELF'].'::'.__FUNCTION__
?>()= $string ?>
print "
if ($var_dump)
{
var_dump($array);
}
else
{
foreach($array as $k => $v)
if (is_bool($v))
$array[$k] = ($v === true) ?
'TRUE':'FALSE';
elseif (is_object($v))
$array[$k] =
'CLASS::'.get_class($v);
print_r( $array );
}
print "\n
?>
}
function debug_print($var, $name='MY_VARIABLE')
{
if (is_array($var))
{
print_x($var, false, $name);
return;
}
echo ''.__FILE__.'@'.__LINE__.''.$name.'
= '.$var."
\n";
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php