## 遍历对象

PHP 提供了一种定义对象的方法使其可以通过单元列表来遍历，例如用 `foreach` 语句。默认情况下，所有可见属性都将被用于遍历。

示例 #1 简单的对象遍历

    <?php
    class MyClass
    {
        public $var1 = 'value 1';
        public $var2 = 'value 2';
        public $var3 = 'value 3';

        protected $protected = 'protected var';
        private   $private   = 'private var';

        function iterateVisible() {
           echo "MyClass::iterateVisible:\n";
           foreach ($this as $key => $value) {
               print "$key => $value\n";
           }
        }
    }

    $class = new MyClass();

    foreach($class as $key => $value) {
        print "$key => $value\n";
    }
    echo "\n";

    $class->iterateVisible();

    ?>

以上示例会输出：

    var1 => value 1
    var2 => value 2
    var3 => value 3

    MyClass::iterateVisible:
    var1 => value 1
    var2 => value 2
    var3 => value 3
    protected => protected var
    private => private var

如上所示，`foreach` 遍历了所有其能够访问的可见属性。

### 参见

- 生成器
- Iterator
- IteratorAggregate
- SPL 迭代器

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open