首页 > 代码库 > PHP 实现var_dump函数
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);
}
PHP 实现var_dump函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。