-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathvar_dump.php
110 lines (106 loc) · 2.71 KB
/
var_dump.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
function my_var_dump()
{
$args = func_num_args();
for ($i = 0; $i < $args; $i++) {
$param = func_get_arg($i);
switch (gettype($param)) {
case 'NULL':
echo 'NULL';
break;
case 'boolean':
echo ($param ? 'bool(true)' : 'bool(false)');
break;
case 'integer':
echo "int($param)";
break;
case 'double':
echo "float($param)";
break;
case 'string':
dump_string($param);
break;
case 'array':
dump_array($param);
break;
case 'object':
dump_object($param);
break;
case 'resource':
echo 'resource';
break;
default:
echo 'UNKNOWN TYPE';
break;
}
echo PHP_EOL;
}
}
function dump_string($param)
{
$str = sprintf("string(%d) %s", strlen($param), $param);
echo $str;
}
function dump_array($param)
{
$len = count($param);
echo "array($len) {" . PHP_EOL;
foreach ($param as $key => $val) {
if (is_array($val)) {
dump_array($val);
} else {
echo sprintf('["%s"] => %s(%s)', $key, gettype($val), $val);
}
echo PHP_EOL;
}
echo "}" . PHP_EOL;
}
function dump_object($param)
{
$className = get_class($param);
$class = new ReflectionClass($param);
$props = $class->getDefaultProperties();
echo sprintf("Object %s #" . spl_object_id($param) . "(%d) {", $className, count($props));
echo PHP_EOL;
foreach ($props as $key => $val) {
$prop = new ReflectionProperty($className, $key);
$access = $prop->isPublic() ? "public" : ($prop->isProtected() ? "protected" : ($prop->isPrivate() ? "private" : ""));
if ($access == "protected" || $access == "private") {
$prop->setAccessible(true);
}
$val = $prop->getValue($param) ? : $val;
echo "[\"$key:$access\"] => ";
my_var_dump($val);
echo PHP_EOL;
}
echo "}";
}
// 打印字符串变量
$name = "学院君";
my_var_dump($name);
echo PHP_EOL;
// 答应数组变量
$arr = [
"Laravel 学院",
"url" => "https://laravelacademy.org"
];
my_var_dump($arr);
echo PHP_EOL;
// 打印对象变量
class MyTestClass
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
}
$obj = new MyTestClass($name);
my_var_dump($obj);
echo PHP_EOL;
// 依次打印多个变量
my_var_dump(1, true, null);