PHP var_dump遍历对象属性的函数与应用代码
更新时间:2010年06月04日 17:00:17 作者:
var_dump此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
本文章下面我们要为你提供二种关于遍历对象属性方法,并且举例说明遍历对象属性在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();
?>
结果如下:
array(2) {
["b"]=>
int(1)
["c"]=>
NULL
}
array(4) {
["a"]=>
NULL
["b"]=>
int(1)
["c"]=>
NULL
["d"]=>
NULL
}
遍历对象属性第二种方法:
<?php
class foo {
private $a;
public $b = 1;
public $c='jb51.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"]=>
string(8) "jb51.net"
}
array(4) {
["a"]=>
NULL
["b"]=>
int(1)
["c"]=>
string(8) "jb51.net"
["d"]=>
NULL
}
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)
*/
?>
遍历对象属性第一种方法:
复制代码 代码如下:
<?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();
?>
结果如下:
array(2) {
["b"]=>
int(1)
["c"]=>
NULL
}
array(4) {
["a"]=>
NULL
["b"]=>
int(1)
["c"]=>
NULL
["d"]=>
NULL
}
遍历对象属性第二种方法:
复制代码 代码如下:
<?php
class foo {
private $a;
public $b = 1;
public $c='jb51.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"]=>
string(8) "jb51.net"
}
array(4) {
["a"]=>
NULL
["b"]=>
int(1)
["c"]=>
string(8) "jb51.net"
["d"]=>
NULL
}
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)
*/
?>
相关文章
解析php中用PHPMailer来发送邮件的示例(126.com的例子)
本篇文章是对php中用PHPMailer来发送邮件的示例(126.com的例子)进行了详细的分析介绍,需要的朋友参考下2013-06-06
最新评论