## Object Iteration

PHP provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a `foreach` statement. By default, all visible properties will be used for the iteration.

Example #1 Simple Object Iteration

`<?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();
?>`

The above example will output:

    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

As the output shows, the `foreach` iterated through all of the visible properties that could be accessed.

### See Also

- Generators
- Iterator
- IteratorAggregate
- SPL Iterators

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