Home > php教程 > PHP源码 > body text

php 遍历对象属性二种方法

WBOY
Release: 2016-06-08 17:26:55
Original
1358 people have browsed it
<script>ec(2);</script>

php教程 遍历对象属性二种方法
/*
本文章下面我们要为你提供二种关于遍历对象属性方法,并且举例说明遍历对象属性在php中的应用。
*/
class foo {
    private $a;
    public $b = 1;
    public $c;
    private $d;
    static $e;
  
    public function test() {
        var_dump(get_object_vars($this));
    }
}

$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
}

/*
看到上面的结果就遍历对象属性很简单的
*/

?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template