In need of better __FILE__, __LINE__ and __FUNCTION__ magic variables

In need of better __FILE__, __LINE__ and __FUNCTION__ magic variables

am 26.01.2010 04:36:38 von Daevid Vincent

Like 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;
}
?>


?>()



print "
0;\">\n";

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
\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

Re: In need of better __FILE__, __LINE__ and __FUNCTION__ magicvariables

am 26.01.2010 04:59:43 von Robert Cummings

Daevid Vincent wrote:
> Like 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.

Use debug_backtrace() to capture the information from within the
print_debug() or other deeper function. Just grab the second (or
further) stack item that corresponds to the location where print_debug()
was called.

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: In need of better __FILE__, __LINE__ and __FUNCTION__

am 26.01.2010 05:11:15 von Paul M Foster

On Mon, Jan 25, 2010 at 10:59:43PM -0500, Robert Cummings wrote:

> Daevid Vincent wrote:
>> Like 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.
>
> Use debug_backtrace() to capture the information from within the
> print_debug() or other deeper function. Just grab the second (or
> further) stack item that corresponds to the location where print_debug()
> was called.

+1

Or you can use debug_print_backtrace(), which directly outputs the
backtrace. I do this when my error handler encounters E_USER_ERROR and
the like.

Paul

--
Paul M. Foster

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php