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);}
时间: 2024-10-12 00:32:39