Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You do not have to do anything special to use <code>foreach</code> with objects. </p> <p>From the <a href="http://de.php.net/manual/en/language.oop5.iterations.php" rel="nofollow noreferrer">PHP manual on Object Iteration</a>:</p> <blockquote> <p>PHP 5 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. </p> </blockquote> <p>Example:</p> <pre><code>class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; } foreach(new Foo as $prop) echo $prop; // outputs 1 only </code></pre> <p>Your class does not have to implement <code>Traversable</code> as suggested elsewhere and in fact, the class above doesn't:</p> <pre><code>var_dump (new Foo instanceof Traversable); // FALSE </code></pre> <p>You <em>can</em> implement one of the <a href="http://de.php.net/manual/en/class.iterator.php" rel="nofollow noreferrer">Iterator</a> or <a href="http://de.php.net/manual/en/class.iteratoraggregate.php" rel="nofollow noreferrer">IteratorAggregate</a> if you need more control over how the iteration should behave: </p> <pre><code>class Foo implements IteratorAggregate { public $foo = 1; protected $bar = 2; private $baz = 3; public function getIterator() { return new ArrayIterator((array) $this); } } foreach(new Foo as $prop) echo $prop; // outputs 123 </code></pre> <p>Because <code>Iterator</code> and <code>IteratorAggregate</code> extend <code>Traversable</code>, your class will now also be an instance of <code>Traversable</code>, but like shown above, it's not necessary for iterating the object.</p> <pre><code>var_dump (new Foo instanceof Traversable); // TRUE </code></pre> <p>You can also use an <a href="http://de.php.net/manual/en/class.arrayobject.php" rel="nofollow noreferrer">ArrayObject</a> to make the object behave like a hybrid between class and object. Or you can implement <a href="http://de.php.net/manual/en/class.arrayaccess.php" rel="nofollow noreferrer">ArrayAccess</a> to allow accessing the class with square brackets. You can also subclass the class to be <a href="http://de.php.net/manual/en/class.arrayiterator.php" rel="nofollow noreferrer">one of the Iterators</a> PHP provides.</p> <p>Further reading:</p> <ul> <li><a href="http://www.phpro.org/tutorials/Introduction-to-SPL.html" rel="nofollow noreferrer">http://www.phpro.org/tutorials/Introduction-to-SPL.html</a></li> <li><a href="http://www.phpro.org/tutorials/Introduction-to-SPL-ArrayAccess.html" rel="nofollow noreferrer">http://www.phpro.org/tutorials/Introduction-to-SPL-ArrayAccess.html</a></li> <li><a href="https://stackoverflow.com/questions/2007695/php-object-like-array">PHP object like array</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload