Home  >  Article  >  Backend Development  >  PHP object-oriented-code sharing for object traversal

PHP object-oriented-code sharing for object traversal

黄舟
黄舟Original
2017-03-25 10:27:151300browse

The traversal of objects is the same as the traversal of arrays. The traversal of objects refers to the traversal of instanceproperties.
 
The attributes traversed below are "accessible attributes" in this scope (access permissions must be considered).

 $value){//$key表示对象的属性,$value是其对应的值
    echo "
属性$key :" . $value; }?>

Run result:

属性p1 :1

It can be seen that only public modified properties can be traversed, so how to traverse all properties of an object? Just write a traversal method inside the class.

 $value){            
        echo "
属性$key :$value"; } } }$obj1 = new A();$obj1->showAllProperties();?>

Run result:

属性p1 :1
属性p2 :2
属性p3 :3

But static properties do not belong to the object, so they cannot be traversed.

The above is the detailed content of PHP object-oriented-code sharing for object traversal. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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