欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php 遍历对象属性二种方法

shiping1 的头像

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));
    }
}

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

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

?>

 

来自 http://www.111cn.net/phper/21/33293.htm

 

 

 

var_dump使用注意事项: 

为了防止程序直接将结果输出到浏览器,可以使用输出控制函数来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。 
var_dump实例代码 

复制代码代码如下:

<?php 
$a = array (1, 2, array ("a", "b", "c")); 
var_dump ($a); 
/* 输出: 
array(3) { 
[0]=> 
int(1) 
[1]=> 
int(2) 
[2]=> 
array(3) { 
[0]=> 
string(1) "a" 
[1]=> 
string(1) "b" 
[2]=> 
string(1) "c" 
} 
} 
*/ 
$b = 3.1; 
$c = TRUE; 
var_dump($b,$c); 
/* 输出: 
float(3.1) 
bool(true) 
*/ 
?>
 
普通分类: