Home  >  Article  >  Backend Development  >  Code application of php var_dump traversing object attributes

Code application of php var_dump traversing object attributes

伊谢尔伦
伊谢尔伦Original
2017-07-08 11:43:001364browse

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=&#39;jb51.net&#39;; 
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!

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