Home >Backend Development >PHP Tutorial >dumprep.exe PHP var_dump function and application code for traversing object attributes

dumprep.exe PHP var_dump function and application code for traversing object attributes

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:42:551069browse

Below in this article, we will provide you with two methods of traversing object properties, and illustrate the application of traversing object properties in PHP. It can be seen that private variables and static variables cannot be obtained, and they can only be read when they are defined as public variables.
The first method of traversing object properties:

Copy the code The code is as follows:


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();
?>


The result is as follows:
array(2) {
["b"]=>
int(1)
["c"]=>
NULL
}
array(4) {
["a"]=>
NULL
["b"]=>
int(1)
["c"]=>
NULL
["d"]=>
NULL
}
Second method of traversing object properties:

Copy code The code is as follows:


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();
?>


The results are as follows:
array(2) {
["b"]=>
int(1)
["c "]=>
string(8) "jb51.net"
}
array(4) {
["a"]=>
NULL
["b"]=>
int(1)
[ "c"]=>
string(8) "jb51.net"
["d"]=>
NULL
}
Notes on using var_dump:
In order to prevent the program from directly outputting the results to the browser, you can use The output control function captures the output of this function and saves them to a variable of type string, for example.
var_dump example code

Copy code The code is as follows:


$a = array (1, 2, array ("a", "b", "c"));
var_dump ($a);
/* Output:
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);
/* Output:
float(3.1)
bool(true)
*/
?>

The above introduces the function and application code of dumprep.exe PHP var_dump to traverse object attributes, including the content of dumprep.exe. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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