PHP var_dump function and application code for traversing object Properties _php Tutorial

Source: Internet
Author: User
In this article we are going to provide you with two ways to iterate over object properties and illustrate the application of traversal object properties in PHP. You can see that private variables and static variables are not available, and can only be read if they are defined as public variables.
The first method of traversing object properties:
Copy CodeThe code is as follows:
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
}
traversing Object Properties The second method:
Copy CodeThe code is as follows:
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 Precautions for use:

To prevent the program from outputting the results directly to the browser, you can use the output control functions to capture the output of this function and save them to a variable such as a string type.
Var_dump Instance Code
Copy CodeThe code is as follows:
$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)
*/
?>

http://www.bkjia.com/PHPjc/322007.html www.bkjia.com true http://www.bkjia.com/PHPjc/322007.html techarticle In This article we are going to provide you with two ways to iterate over object properties and illustrate the application of traversal object properties in PHP. You can see that private variables and static variables get not ...

  • Related Article

    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.