The traversal of an object, like the traversal of a group, refers to the traversal of an instance property .
The properties that are traversed below are "accessible properties" in the scope (to consider access rights).
<?php
class a{public
$p 1 = 1;
Protected $p 2 = 2;
Private $p 3 = 3;
Static $p 4 = 4;
}
$obj 1 = new A ();
foreach ($obj 1 as $key => $value) {//$key represents the properties of the object, $value its corresponding value,
echo "<br/> Property $key:". $value
;
>
Run Result:
It is visible that only the properties of the public adornment can be traversed, that is, how to make all the properties of an object traverse. Write a traversal method inside the class.
<?php
class a{public
$p 1 = 1;
Protected $p 2 = 2;
Private $p 3 = 3;
Static $p 4 = 4; static property
function Showallproperties () {
foreach ($this as $key => $value) {
echo <br/> Property $key: $ Value ';
}
}} $obj 1 = new A ();
$obj 1->showallproperties ();
? >
Run Result:
Property P1:1
Property P2:2
However, static properties are not part of the object, so they cannot be traversed.