PHP Object-oriented code sharing for object-based traversal

Source: Internet
Author: User
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.

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.