'.self::getDisplayDump($var).'
';
}
}
/**
* Prints a dump of the public, protected and private properties of $var.
*
* @param mixed $var
* @param integer $maxDepth Maximum nesting level for object properties
* @param boolean $stripTags Flag that indicate if output should strip HTML tags
*/
static function dump($var, $maxDepth = 2, $stripTags = false)
{
$var = self::export($var, $maxDepth++,$stripTags);
if ($stripTags) {
print var_export($var,true);
} else {
print ''.self::getDisplayDump($var).'
';
}
}
public static function getDisplayDump($dump)
{
$return = '';
if (is_array($dump['value'])) {
$return .= $dump['type'];
$return .= '';
foreach ($dump['value'] as $i =>$val) {
$return .= '- ';
$return .=''.$i.' => '.self::getDisplayDump($val);
$return .= '
';
}
$return .= '
';
} else {
if (is_null($dump['value'])) {
$return .= $dump['type'];
} else {
$return .= $dump['type'] .' "'. htmlentities(substr($dump['value'],0,200)).'"';
}
}
return $return;
}
/**
* Export
*
* @param mixed $var
* @param int $maxDepth
* @return mixed
*/
public static function export($var, $maxDepth = 2,$striptags = false)
{
$aReturn = array('type' => '', 'value' => $var);
if ($striptags) {
//text only version
if (is_array($var)) {
$aReturn['type'] = ' Array (length ' . count($var) . ') ';
} else if (is_object($var)) {
$aReturn['type'] = 'Object (class ' . get_class($var) . ') ';
} else {
$aReturn['type'] = gettype($var);
}
if ($maxDepth === 0) {
if (is_object($var) || is_array($var)) {
return $aReturn['type'];
} else {
return $aReturn['value'];
}
}
} else {
//html version
if (is_array($var)) {
$aReturn['type'] = 'Array (length ' . count($var) . ')';
} else if (is_object($var)) {
$aReturn['type'] = 'Object (class ' . get_class($var) . ')';
} else {
$aReturn['type'] = ''.gettype($var).'';
}
if ($maxDepth === 0) {
if (is_object($var) || is_array($var)) {
$aReturn['value'] = null;
}
return $aReturn;
}
}
if (is_array($var)) {
$aReturn['value'] = array();
foreach ($var as $k => $v) {
$aReturn['value'][$k] = self::export($v, $maxDepth - 1,$striptags);
}
} else if (is_object($var)) {
$aReturn['value'] = array();
$var = self::getProperties($var);
foreach ($var as $k => $v) {
$aReturn['value'][$k] = self::export($v, $maxDepth - 1,$striptags);
}
} else {
$aReturn['value'] = $var;
}
if ($striptags) {
return $aReturn['value'];
}
return $aReturn;
}
protected static function getProperties($object)
{
$class = get_class($object);
$resArray = array();
$reflection = new ReflectionObject($object);
$properties = $reflection->getProperties();
foreach ($properties as $attr) {
$attr->setAccessible(true);
$resArray[implode(' ', Reflection::getModifierNames($attr->getModifiers())) . ' $' . $attr->name] = $attr->getValue($object);
}
return $resArray;
}
}
?>