本文章下面我们要为你提供二种关于遍历对象属性方法,并且举例说明遍历对象属性在php中的应用。可以看出私有变量与静态变量时获取不到的,只有定义为公共变量才可以读出来。
遍历对象属性第一种方法:
<BR><?php <BR>class foo { <BR>private $a; <BR>public $b = 1; <BR>public $c; <BR>private $d; <BR>static $e; <BR>public function test() { <BR>var_dump(get_object_vars($this)); <BR>} <BR>} <BR>$test = new foo; <BR>var_dump(get_object_vars($test)); <BR>$test->test(); <BR>?> <BR>
结果如下:
array(2) {
[“b”]=>
int(1)
[“c”]=>
NULL
}
array(4) {
[“a”]=>
NULL
[“b”]=>
int(1)
[“c”]=>
NULL
[“d”]=>
NULL
}
遍历对象属性第二种方法:
<BR><?php <BR>class foo { <BR>private $a; <BR>public $b = 1; <BR>public $c='jb51.net'; <BR>private $d; <BR>static $e; <BR>public function test() { <BR>var_dump(get_object_vars($this)); <BR>} <BR>} <BR>$test = new foo; <BR>var_dump(get_object_vars($test)); <BR>$test->test(); <br><br>?> <br><br>
结果如下:
array(2) {
[“b”]=>
int(1)
[“c”]=>
string(8) “jb51.net”
}
array(4) {
[“a”]=>
NULL
[“b”]=>
int(1)
[“c”]=>
string(8) “jb51.net”
[“d”]=>
NULL
}
var_dump使用注意事项:
为了防止程序直接将结果输出到浏览器,可以使用输出控制函数来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。
var_dump实例代码
<BR><?php <BR>$a = array (1, 2, array ("a", "b", "c")); <BR>var_dump ($a); <BR>/* 输出: <BR>array(3) { <BR>[0]=> <BR>int(1) <BR>[1]=> <BR>int(2) <BR>[2]=> <BR>array(3) { <BR>[0]=> <BR>string(1) "a" <BR>[1]=> <BR>string(1) "b" <BR>[2]=> <BR>strin<strong style="color:transparent">9来源gaodai#ma#com搞@代~码$网</strong>搞gaodaima代码g(1) "c" <BR>} <BR>} <BR>*/ <BR>$b = 3.1; <BR>$c = TRUE; <BR>var_dump($b,$c); <BR>/* 输出: <BR>float(3.1) <BR>bool(true) <BR>*/ <BR>?> <BR>