Home > Article > Backend Development > Code application of php var_dump traversing object attributes
var_dump This function displays structural information about one or more expressions, including the type and value of the expression. Arrays will expand values recursively, showing their structure through indentation.
Below in this article we will provide you with two methods about traversing objects properties , and give examples of 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:
The code is as follows:
<?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(); ?>
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:
The code is as follows:
<?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(); ?>
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
}
var_dump usageNotes:
In order to prevent the program from outputting the results directly to the browser, you can use the output control function to capture this The output of the function and save them to a variable of type string, for example.
var_dump example code
The code is as follows:
<?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) */ ?>
The above is the detailed content of Code application of php var_dump traversing object attributes. For more information, please follow other related articles on the PHP Chinese website!