Var_dump this function displays the structure information about one or more expressions, including the expression type and value. The array recursively expands the value and displays its structure through indentation. In this article, we will provide you with two methods for traversing object attributes, and illustrate the application of traversing object attributes in php. We can see that private variables and static variables cannot be obtained. only public variables can be read.
The first method to traverse object attributes:
The code is as follows:
Class foo {
Private $;
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
}
The second method to traverse object attributes:
The code is as follows:
Class foo {
Private $;
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 result is 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 var_dump:
To prevent the program from outputting the results directly to the browser, you can use the output control function to capture the output of this function and save them to a variable of the string type, for example.
Var_dump instance code
The code is as follows:
$ A = array (1, 2, array ("a", "B", "c "));
Var_dump ($ );
/* Output:
Array (3 ){
[0] =>
Int (1)
[1] =>
Int (2)
[2] =>
Array (3 ){
[0] =>
String (1) ""
[1] =>
String (1) "B"
[2] =>
String (1) "c"
}
}
*/
$ B = 3.1;
$ C = TRUE;
Var_dump ($ B, $ c );
/* Output:
Float (3.1)
Bool (true)
*/
?>