PHP 自己实现var_dump函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 自己实现var_dump函数相关的知识,希望对你有一定的参考价值。
private static $isInLoop = false;
private static $buffer = false;
public static function dumps()
{
$args = func_get_args();
$args_num = func_num_args();
if (self::$buffer && $args_num == 1) {
$args = func_get_args();
$args = isset($args[0]) ? $args[0] : [];
if (!empty($args) && (is_array($args) || is_object($args))) {
$args_num = count($args);
}
}
self::$buffer = false;
for ($i = 0; $i < $args_num; ++$i) {
$param = $args[$i];
$ptype = gettype($param);
switch ($ptype) {
case "NULL":
self::dump_null();
break;
case "boolean":
self::dump_boolean($param);
break;
case "integer":
self::dump_integer($param);
break;
case "double":
self::dump_double($param);
break;
case "string":
self::dump_string($param);
break;
case "array":
self::dump_array($param);
break;
case "object":
self::dump_object($param);
break;
case "resource":
echo "resource";
break;
default:
echo "unknown type";
}
}
}
public static function dumpx()
{
self::$buffer = true;
ob_start();
self::dumps(func_get_args());
$var = ob_get_clean();
return $var;
}
public static function dump_null()
{
echo "NULL";
if (!self::$isInLoop) {
echo "\n";
}
self::$isInLoop = false;
}
public static function dump_boolean($bool)
{
if ($bool) {
echo "bool(true)";
} else {
echo "bool(false)";
}
if (!self::$isInLoop) {
echo "\n";
}
self::$isInLoop = false;
}
public static function dump_integer($int)
{
echo "int($int)";
if (!self::$isInLoop) {
echo "\n";
}
self::$isInLoop = false;
}
public static function dump_double($double)
{
echo "float($double)";
if (!self::$isInLoop) {
echo "\n";
}
self::$isInLoop = false;
}
public static function dump_string($str)
{
$len = strlen($str);
$value = "string($len) \"$str\"";
echo $value;
if (!self::$isInLoop) {
echo "\n";
}
self::$isInLoop = false;
}
public static function dump_array($arr)
{
static $pads = [];
$keys = array_keys($arr);
$len = count($arr);
echo "array($len) {";
array_push($pads, " ");
for ($i = 0; $i < $len; $i++) {
echo "\n", implode(‘‘, $pads), "[\"$keys[$i]\"] => ";
$index = $keys[$i];
self::$isInLoop = true;
self::dumps($arr[$index]);
}
array_pop($pads);
$pad = implode(‘‘, $pads);
echo "\n{$pad}}";
if ($pad == ‘‘) {
echo "\n";
}
}
public static function dump_prop($obj)
{
static $pads = [];
$reflect = new ReflectionClass($obj);
$prop = $reflect->getProperties();
$len = count($prop);
echo "($len) {";
array_push($pads, " ");
for ($i = 0; $i < $len; $i++) {
$index = $i;
if (!$prop[$index]->isPublic()) {
continue;
}
$prop_name = $prop[$index]->getName();
echo "\n", implode(‘‘, $pads), "[\"{$prop_name}\"] => ";
self::$isInLoop = true;
self::dumps($prop[$index]->getValue($obj));
}
array_pop($pads);
$pad = implode(‘‘, $pads);
echo "\n{$pad}}";
if ($pad == ‘‘) {
echo "\n";
}
}
public static function dump_object($obj)
{
static $objId = 1;
$className = get_class($obj);
echo "object($className)#$objId";
$objId++;
self::dump_prop($obj);
}
dumps(new class(),"abc",111);
本文出自 “11130372” 博客,请务必保留此出处http://11140372.blog.51cto.com/11130372/1916106
以上是关于PHP 自己实现var_dump函数的主要内容,如果未能解决你的问题,请参考以下文章