TVarDumper 旨在替换有缺陷的 PHP 函数var_dump
,print_r
因为它可以正确识别复杂对象结构中递归引用的对象。它还具有递归深度控制,以避免某些特殊变量的无限递归显示。
检查TVarDumper.php
:
<?php
/**
* TVarDumper class file
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005-2013 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Id$
* @package System.Util
*/
/**
* TVarDumper class.
*
* TVarDumper 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.
*
* TVarDumper can be used as follows,
* <code>
* echo TVarDumper::dump($var);
* </code>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package System.Util
* @since 3.0
*/
class TVarDumper
{
private static $_objects;
private static $_output;
private static $_depth;
/**
* Converts a variable into a string representation.
* This method achieves the similar functionality as var_dump and print_r
* but is more robust when handling complex objects such as PRADO controls.
* @param mixed variable to be dumped
* @param integer maximum depth that the dumper should go into the variable. Defaults to 10.
* @return string the string representation of the variable
*/
public static function dump($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);
return preg_replace('/<\\?php<br \\/>/','',$result,1);
}
else
return self::$_output;
}
private static function dumpInternal($var,$level)
{
switch(gettype($var))
{
case 'boolean':
self::$_output.=$var?'true':'false';
break;
case 'integer':
self::$_output.="$var";
break;
case 'double':
self::$_output.="$var";
break;
case 'string':
self::$_output.="'$var'";
break;
case 'resource':
self::$_output.='{resource}';
break;
case 'NULL':
self::$_output.="null";
break;
case 'unknown type':
self::$_output.='{unknown}';
break;
case 'array':
if(self::$_depth<=$level)
self::$_output.='array(...)';
else if(empty($var))
self::$_output.='array()';
else
{
$keys=array_keys($var);
$spaces=str_repeat(' ',$level*4);
self::$_output.="array\n".$spaces.'(';
foreach($keys as $key)
{
self::$_output.="\n".$spaces." [$key] => ";
self::$_output.=self::dumpInternal($var[$key],$level+1);
}
self::$_output.="\n".$spaces.')';
}
break;
case 'object':
if(($id=array_search($var,self::$_objects,true))!==false)
self::$_output.=get_class($var).'#'.($id+1).'(...)';
else if(self::$_depth<=$level)
self::$_output.=get_class($var).'(...)';
else
{
$id=array_push(self::$_objects,$var);
$className=get_class($var);
$members=(array)$var;
$keys=array_keys($members);
$spaces=str_repeat(' ',$level*4);
self::$_output.="$className#$id\n".$spaces.'(';
foreach($keys as $key)
{
$keyDisplay=strtr(trim($key),array("\0"=>':'));
self::$_output.="\n".$spaces." [$keyDisplay] => ";
self::$_output.=self::dumpInternal($members[$key],$level+1);
}
self::$_output.="\n".$spaces.')';
}
break;
}
}
}
XDebug var_dump
使用 XDebug PHP 扩展,它会检测并忽略循环引用,例如:
echo xdebug_var_dump($object);
print_r
+array_slice
根据这篇文章,您可以尝试:
print_r(array_slice($desiredArray, 0, 4));
features_var_export
使用以下功能,它是Drupal ( ) 的功能features.export.inc
模块的一部分:
/**
* Export var function
*/
function features_var_export($var, $prefix = '', $init = TRUE, $count = 0) {
if ($count > 50) {
// Recursion depth reached.
return '...';
}
if (is_object($var)) {
$output = method_exists($var, 'export') ? $var->export() : features_var_export((array) $var, '', FALSE, $count+1);
}
else if (is_array($var)) {
if (empty($var)) {
$output = 'array()';
}
else {
$output = "array(\n";
foreach ($var as $key => $value) {
// Using normal var_export on the key to ensure correct quoting.
$output .= " " . var_export($key, TRUE) . " => " . features_var_export($value, ' ', FALSE, $count+1) . ",\n";
}
$output .= ')';
}
}
else if (is_bool($var)) {
$output = $var ? 'TRUE' : 'FALSE';
}
else if (is_int($var)) {
$output = intval($var);
}
else if (is_numeric($var)) {
$floatval = floatval($var);
if (is_string($var) && ((string) $floatval !== $var)) {
// Do not convert a string to a number if the string
// representation of that number is not identical to the
// original value.
$output = var_export($var, TRUE);
}
else {
$output = $floatval;
}
}
else if (is_string($var) && strpos($var, "\n") !== FALSE) {
// Replace line breaks in strings with a token for replacement
// at the very end. This protects whitespace in strings from
// unintentional indentation.
$var = str_replace("\n", "***BREAK***", $var);
$output = var_export($var, TRUE);
}
else {
$output = var_export($var, TRUE);
}
if ($prefix) {
$output = str_replace("\n", "\n$prefix", $output);
}
if ($init) {
$output = str_replace("***BREAK***", "\n", $output);
}
return $output;
}
用法:
echo features_var_export($object);
连载
用于serialize
以序列化表示形式转储对象,例如:
echo serialize($object);
JSON编码
用于json_encode
将其转换为 JSON 格式,例如:
echo json_encode($object);
另请参阅:测试变量是否包含循环引用