SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    var_dump enhancement?

    Has someone written a replacement, or enhancement, of var_dump? I am using xdebug's version, but is anything else available?

  2. #2
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Here is one I wrote earlier

    http://johns-jokes.com/downloads/_fred.txt

    It works with boolean, strings, arrays, object, etc

    PHP Code:

    # Usage
     
    require '_fred.php';

    $a=array
    (
      
    'Tom',
      
    'Dick',
      
    'Harry',
    );

    fred$a 'optional title goes here');

    # output:
    Name:  optional title goes here

    Type 
    ==> array(3)
    Count  ==> 3
    Value  
    ==> 
    Array
    (
        [
    0] => Tom
        
    [1] => Dick
        
    [2] => Harry

    Last edited by John_Betong; Feb 25, 2012 at 10:30. Reason: syntax error
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  3. #3
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    I have been using the Zend Framework 1 Debug class for many years. You can pull it right out of the framework with out any difficulty. It does a good job of formatting things and seems to handle circular reference in a rational fashion.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    What else do you want from an improved var_dump()?

  5. #5
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    This supports both cli and web.
    PHP Code:

    /* ------------------------------
     * Basically copied from an old version of the Zend Framework 1
     * The debug routine is very handy
     */
    namespace Zayso\CoreBundle\Component;

    class 
    Debug
    {

        
    /**
         * @var string
         */
        
    protected static $_sapi null;

        
    /**
         * Get the current value of the debug output environment.
         * This defaults to the value of PHP_SAPI.
         *
         * @return string;
         */
        
    public static function getSapi()
        {
            if (
    self::$_sapi === null) {
                
    self::$_sapi PHP_SAPI;
            }
            return 
    self::$_sapi;
        }

        
    /**
         * Set the debug ouput environment.
         * Setting a value of null causes Zend_Debug to use PHP_SAPI.
         *
         * @param string $sapi
         * @return void;
         */
        
    public static function setSapi($sapi)
        {
            
    self::$_sapi $sapi;
        }

        
    /**
         * Debug helper function.  This is a wrapper for var_dump() that adds
         * the <pre /> tags, cleans up newlines and indents, and runs
         * htmlentities() before output.
         *
         * @param  mixed  $var   The variable to dump.
         * @param  string $label OPTIONAL Label to prepend to output.
         * @param  bool   $echo  OPTIONAL Echo output if true.
         * @return string
         */
        
    public static function dump($var$label=null$echo=true)
        {
            
    // format the label
            
    $label = ($label===null) ? '' rtrim($label) . ' ';

            
    // var_dump the variable into a buffer and keep the output
            
    ob_start();
            
    var_dump($var);
            
    $output ob_get_clean();

            
    // neaten the newlines and indents
            
    $output preg_replace("/\]\=\>\n(\s+)/m""] => "$output);
            if (
    self::getSapi() == 'cli') {
                
    $output PHP_EOL $label
                        
    PHP_EOL $output
                        
    PHP_EOL;
            } else {
                
    $output '<pre>'
                        
    $label
                        
    htmlspecialchars($outputENT_QUOTES)
                        . 
    '</pre>';
            }

            if (
    $echo) {
                echo(
    $output);
            }
            return 
    $output;
        }



  6. #6
    SitePoint Zealot
    Join Date
    Nov 2011
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for the replies and example code. Its given me a lot to think about. I had done some of the things John Betong did in his code, but didn't nearly take it nearly as far or as comprehensively. I haven't used Zend, so I will take a look at it that, ahundiak.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •