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.php 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 '<b>'.$_SERVER['PHP_SELF'].'::'.__FUNCTION__."(Not An
Array) $string</b><br/>\n";
                return;
        }
?>
<div class="debug">
        <div class="header"><h2><?= $_SERVER['PHP_SELF'].'::'.__FUNCTION__
?>()<?= $string ?></h2></div>
        <div class="content">
        <?php
            print "<div class=\"debug\"><pre style=\"padding: 0; margin:
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 "</pre>\n</div>\n";
        ?>
        </div>
</div>
<?php
}

function debug_print($var, $name='MY_VARIABLE')
{
        if (is_array($var))
        {
                print_x($var, false, $name);
                return;
        }

        echo '<span class="debug">'.__FILE__.'@'.__LINE__.'<b>'.$name.'</b>
= '.$var."</span><br/>\n";
}


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

Reply via email to