PHP var_dump traverses the object properties of the function and the application code

  • 2020-03-31 20:47:56
  • OfStack

In this article, we will provide you with two methods of traversing object properties, and illustrate the application of traversing object properties in PHP. You can see that private variables and static variables are not available, only defined as public variables can be read.
The first way to iterate over an object's properties:
 
<?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 results are as follows:
Array (2) {
[" b "] = >
Int (1)
[" c "] = >
NULL
}
Array (4) {
[" a "] = >
NULL
[" b "] = >
Int (1)
[" c "] = >
NULL
[" d "] = >
NULL
}
The second way to iterate over an object's properties:
 
<?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
}

Notes for using var_dump:

To prevent the program from printing the result directly to the browser, you can use the output control function to capture the output of this function and save it to a variable of type string, for example.
Var_dump instance code
 
<?php 
$a = array (1, 2, array ("a", "b", "c")); 
var_dump ($a); 
 
$b = 3.1; 
$c = TRUE; 
var_dump($b,$c); 
 
?> 

Related articles: