Home > Backend Development > PHP Tutorial > PHP var_dump function and application code for traversing object attributes_PHP tutorial

PHP var_dump function and application code for traversing object attributes_PHP tutorial

WBOY
Release: 2016-07-21 15:37:05
Original
823 people have browsed it

Below in this article we will provide you with two methods of traversing object properties, and illustrate the application of traversing object properties in PHP. It can be seen that private variables and static variables cannot be obtained, and they can only be read when they are defined as public variables.
The first method of traversing object properties:

Copy code The code is 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 The 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 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 on using var_dump:

In order 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 type string, for example.
var_dump example code
Copy code The 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)
*/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322007.htmlTechArticleIn this article, we will provide you with two methods of traversing object properties, and give you an example of traversing object properties in php applications in. It can be seen that private variables and static variables cannot be obtained...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template