Versions
Look up a class, method, property or event

CVarDumper

Package system.utils
Inheritance class CVarDumper
Since 1.0
Source Code framework/utils/CVarDumper.php
CVarDumper is intended to replace the buggy PHP function var_dump and print_r. It can correctly identify the recursively referenced objects in a complex object structure. It also has a recursive depth control to avoid indefinite recursive display of some peculiar variables.

CVarDumper can be used as follows,
CVarDumper::dump($var);

Public Methods

Hide inherited methods

MethodDescriptionDefined By
dump() Displays a variable. CVarDumper
dumpAsString() Dumps a variable in terms of a string. CVarDumper

Method Details

dump() method
public static void dump(mixed $var, integer $depth=10, boolean $highlight=false)
$var mixed variable to be dumped
$depth integer maximum depth that the dumper should go into the variable. Defaults to 10.
$highlight boolean whether the result should be syntax-highlighted
Source Code: framework/utils/CVarDumper.php#40 (show)
public static function dump($var,$depth=10,$highlight=false)
{
    echo 
self::dumpAsString($var,$depth,$highlight);
}

Displays a variable. This method achieves the similar functionality as var_dump and print_r but is more robust when handling complex objects such as Yii controllers.

dumpAsString() method
public static string dumpAsString(mixed $var, integer $depth=10, boolean $highlight=false)
$var mixed variable to be dumped
$depth integer maximum depth that the dumper should go into the variable. Defaults to 10.
$highlight boolean whether the result should be syntax-highlighted
{return} string the string representation of the variable
Source Code: framework/utils/CVarDumper.php#54 (show)
public static function dumpAsString($var,$depth=10,$highlight=false)
{
    
self::$_output='';
    
self::$_objects=array();
    
self::$_depth=$depth;
    
self::dumpInternal($var,0);
    if(
$highlight)
    {
        
$result=highlight_string("<?php\n".self::$_output,true);
        
self::$_output=preg_replace('/&lt;\\?php<br \\/>/','',$result,1);
    }
    return 
self::$_output;
}

Dumps a variable in terms of a string. This method achieves the similar functionality as var_dump and print_r but is more robust when handling complex objects such as Yii controllers.

Total 1 comment

#16227 report it
Muhammad Rafiq at 2014/01/31 04:58pm
A simple way to dump variable in readable format

I find that the dump() method as well as print_r does not give a readable format. For my project, I use this simple function to provide proper formatting when dumping variable. I borrow this method from Moodle eLearning system.

public function printObject($object, $ret=FALSE)
{
        if(!$ret)
            echo '<pre>' . htmlspecialchars(print_r($object,true)) . '</pre>';
        else
            return '<pre>' . htmlspecialchars(print_r($object,true)) . '</pre>';        
}

This method has been my most handy function to dump any variable or object and the output is very readable.

Leave a comment

Please to leave your comment.