The traversal of an object is the same as the traversal of an object, which refers to the traversal of an
instance property .
The properties that are traversed below are the "accessible attributes" in that scope (to consider access rights).
<?phpclass 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 object's properties, $value is its corresponding value echo "<br/> Property $key:". $value
}?>
Operation Result:
Property P1:1
It is visible that only public-decorated properties can traverse, so how do you traverse all the properties of an object? Write a traversal method inside the class.
<?phpclass 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 ();? >
Operation Result:
Property P1:1 Property P2:2 Property P3:3
However, static properties do not belong to the object, so they cannot be traversed.