Profile Class to analyze Arrays / Objects

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
joshmaker
Forum Commoner
Posts: 25
Joined: Mon May 15, 2006 2:53 pm
Location: Arlington VA

Profile Class to analyze Arrays / Objects

Post by joshmaker »

This is a profile class I wrote to analyze Arrays / Objects for debugging purposes. Basically, I wanted a nice alternative to writing print_r($myArray)

Code: Select all

<?php
 
class Profile
{
    static $alt;
    static $depth;
    
    /**
     * Echo out a profile of the variable in a nice table
     *
     * @param string $var the variable to analyze
     * @param string $note an optional caption for the table
     * @return void
     */
    static function info( $var, $note=null )
    {   
        self::$depth = 1;
 
        echo '<br /><br />' . self::getInfoTable( $var, $note );
    }
    
    /**
     * Profile off current $_POST data
     *
     * @return void
     */
    static function post()
    {
        if( isset($_POST) ) self::info($_POST, '$_POST Data');
        else echo '$_POST is not set';
    }
    
    /**
     * Profile off current $_GET data
     *
     * @return void
     */
    static function get()
    {
        if( isset($_GET) ) self::info($_GET, '$_GET Data');
        else echo '$_GET is not set';
    }
    
    /**
     * Profile off current $_SESSION data
     *
     * @return void
     */
    static function session()
    {
        if( isset($_SESSION) )  self::info($_SESSION, '$_SESSION Data');
        else echo '$_SESSION is not set';
    }
    
    /**
     * Profile off current $_SERVER data
     *
     * @return void
     */
    static function server()
    {
        if( isset($_SERVER) ) self::info($_SERVER, '$_SERVER Data');
        else echo '$_SERVER is not set';
    }
    
    /**
     * Profile off current $_POST, $_GET, $_SESSION, & $_SERVER data
     *
     * @return void
     */
    static function all()
    {
        self::session();
        self::post();
        self::get();
        self::server();     
    }
    
    /**
     * make a table with information about the current variable, if the variable contains an object / array
     * run that object / array through this function recursively to make another table (max depth = 25)
     *
     * @param string $var 
     * @param string $note 
     * @return void
     * @author TMI
     */
    static function getInfoTable( $var, $note = null )
    {   
        self::$depth = self::$depth + 1;
        if( self::$depth > 25 ) {
            echo 'Recursive analysis existed to prevent possible endless loop';
            return '';
        }
        
        if( is_object($var) ) $varclass = get_class($var);
        
        $var = (array) $var;
 
            
        $html = 
        '
        <table style="border: solid 1px black;">
            <caption style="text-align: center; font-weight: bold">' . $note . ' (<u style="text-decoration: underline;">' . $varclass . '</u> '. gettype($var) . ')</caption>
            <tr style="font-weight: bold;">
                <th style="background: #D8D8D8; padding: 2px">Key</th>
                <th style="background: #D8D8D8; padding: 2px">Type</th>
                <th style="background: #D8D8D8; padding: 2px">Value</th>
            </tr>';
        
 
        foreach( $var as $key => $value )
        {
            if( self::$alt == "#EDEDED" ) self::$alt = "#FFF";
            else self::$alt = "#EDEDED";
                        
            if( is_array($value) || is_object($value) )
                $listedValue = Profile::getInfoTable($value);
            else
                $listedValue = htmlentities($value);
            
            $html.='
            <tr>
                <td style="background: #A0A0A0; text-align: right; padding: 3px; color: white">' . $key . '</td>
                <td style="background: ' . self::$alt . '; padding: 3px; font-style: italic; ">' . ' ' . gettype($value) . '</td>
                <td style="background: ' . self::$alt . '; padding: 3px;">' . $listedValue . '</td>
            </tr>';
        }
        
        $html .= '</table>';
        
        return $html;
    }
}
Here is how it would work:

Code: Select all

<?php 
// to examine an array or an object
Profile::info( $myVar );
 
// equivalent to writing Profile::info( $_POST );
Profile::post();
 
// equivalent to writing Profile::info( $_SESSION );
Profile::session();
 
// etc...
 
Think I'm on to something useful? Or am I just wasting my time?
ioan1k
Forum Newbie
Posts: 8
Joined: Thu Nov 06, 2008 12:48 pm

Re: Profile Class to analyze Arrays / Objects

Post by ioan1k »

if you want to format everything nicely this could be useful or you can just use a function along the lines of

Code: Select all

 
function varDump($var)
{
    echo '<pre>';
    var_dump($var);
    echo '</pre>';
}
 
you just wont get the table layout and the colors, it just depends on how well you want your debugging formatted

Also when i ran this with Zend and parsed out my Acl System object ( extremely large ) it dumped the information but the way it was formatted, and listed almost impossible to read as all the info was squished down into tiny little tables.

Work on it some, format everything better, maybe make the colors configurable and I wouldn't mind using this :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Profile Class to analyze Arrays / Objects

Post by alex.barylski »

Debugging isn't really related to debugging...although your code is neat if all you are concerned in doing is seeing the print_r() results this is what I do:

Code: Select all

abstract class Debug_Object{
  function __toString()
  {
    echo '<pre>';
    print_r($this);
  }
}
Then you simply derive all classes you want debug info on from this class, like so:

Code: Select all

class MyObject extends Debug_Object{
  function setValue($value){ $this->_value = $value; }
}
You then implement your objects as you normally would and use them as expected:

Code: Select all

$obj = new MyObject();
$obj->setValue('This is the objects new value');
Now because of the way __toString() works all you need to do to see a full dump of any object derived from Debug_Object is echo the object like so:

Code: Select all

echo $obj;
PHP will call the __toString() magic function when an object is used like this and you will see a nice dump of your object and it entirety.

Saves you tons of writing out the code each time you want to test an object doing:

Code: Select all

echo '<pre>';
print_r($someObject);
echo '</pre>';
The all I do is strip the inheritence from Debug_Object on live code so there is no overhead...

Cheers,
Alex
Post Reply