本文章下面我們要為你提供二種關于遍歷對象屬性方法,并且舉例說明遍歷對象屬性在php中的應用.
- <?php
- ?//方法一
- class foo {
- private $a;
- public $b = 1;
- public $c;
- private $d;
- static $e;
- public function test() {
- var_dump(get_object_vars($this));
- }
- }
- //開源代碼Vevb.com
- $test = new foo;
- var_dump(get_object_vars($test));
- $test->test();
- //方法二
- class foo {
- private $a;
- public $b = 1;
- public $c='111cn.net';
- private $d;
- static $e;
- public function test() {
- var_dump(get_object_vars($this));
- }
- }
- $test = new foo;
- var_dump(get_object_vars($test));
- $test->test();
- //結果如下:
- array(2) {
- ["b"]=>
- int(1)
- ["c"]=>
- 111cn.net
- }
- array(4) {
- ["a"]=>
- NULL
- ["b"]=>
- int(1)
- ["c"]=>
- 111cn.net
- ["d"]=>
- NULL
- }
- /*
- 看到上面的結果就遍歷對象屬性很簡單的
- */
- ?>
新聞熱點
疑難解答