PHPvar_dump: Function and application code used to traverse object attributes

Source: Internet
Author: User
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)
*/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.