首页 > php面向对象 > php 遍历对象属性二种方法

php 遍历对象属性二种方法

本文章下面我们要为你提供二种关于遍历对象属性方法,并且举例说明遍历对象属性在php中的应用.

<?php 
//方法一 
class foo { 
    private $a; 
    public $b = 1; 
    public $c; 
    private $d; 
    static $e; 
    
    public function test() { 
        var_dump(get_object_vars($this)); 
    } 
} 
//开源代码phprm.com 
$test = new foo; 
var_dump(get_object_vars($test)); 
 
$test->test(); 
 
 
//方法二 
class foo { 
    private $a; 
    public $b = 1; 
    public $c='phprm.com'; 
    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"]=> 
  phprm.com 
} 
array(4) { 
  ["a"]=> 
  NULL 
  ["b"]=> 
  int(1) 
  ["c"]=> 
  phprm.com  
  ["d"]=> 
  NULL 
} 
 
/* 
看到上面的结果就遍历对象属性很简单的 
*/


本文链接:http://www.phprm.com/mxdx/fs4378.html

收藏随意^^请保留教程地址.

标签:php遍历对象 php对象属性

发表留言