In this article we will provide you with two kinds of methods about traversing object properties, and illustrate the application of traversal object properties in PHP. You can see that the private variable and the static variable are not available, only defined as public variables can be read out.
The first method of traversing object properties:
Copy Code code 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 results are as follows:
Array (2) {
["B"]=>
Int (1)
["C"]=>
Null
}
Array (4) {
["A"]=>
Null
["B"]=>
Int (1)
["C"]=>
Null
["D"]=>
Null
}
The second method of traversing object properties:
Copy Code code 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 Use precautions:
To prevent programs from outputting results directly to the browser, you can use output control functions to capture the output of this function and save them to a variable of type string.
Var_dump Instance Code
Copy Code code as follows:
<?php
$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)
/*
?>