Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13755

Re: ArrayAccess interface, Traversable interface and foreach

From Adam Harvey <usenet@adamharvey.name>
Newsgroups comp.lang.php
Subject Re: ArrayAccess interface, Traversable interface and foreach
Date 2014-05-27 18:59 +0000
Organization A noiseless patient Spider
Message-ID <lm2n9m$1i2$1@dont-email.me> (permalink)
References <fdd1f0af-6cbf-4577-a4c4-7c9073eae483@googlegroups.com> <53806ec6$0$6713$9b4e6d93@newsspool2.arcor-online.net>

Show all headers | View raw


On Sat, 24 May 2014 12:05:02 +0200, Christoph Michael Becker wrote:

> kurtk@pobox.com wrote:
> 
>> This seems like an oversight in the documentation--unless I am missing
>> something?
> 
> You can use any object (whether it is an instance of a class that
> implements Traversable or not) in a foreach loop, see
> <http://www.php.net/manual/en/control-structures.foreach.php>.

Yep, this is why objects that implement ArrayAccess still work with 
foreach without implementing Traversable.

ArrayAccess and Traversable are technically different things: Traversable 
provides a way to control iteration, whereas ArrayAccess is only for 
array-style dereferencing via $object['key']. In practice, classes that 
need to imitate arrays will need to implement both (probably by extending 
a class such as ArrayObject that implements both interfaces).

Adam

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

ArrayAccess interface, Traversable interface and foreach "kurtk@pobox.com" <kurtk@pobox.com> - 2014-05-23 11:22 -0700
  Re: ArrayAccess interface, Traversable interface and foreach Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-23 16:35 -0400
  Re: ArrayAccess interface, Traversable interface and foreach Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-24 12:05 +0200
    Re: ArrayAccess interface, Traversable interface and foreach Jerry Stuckle <jstucklex@attglobal.net> - 2014-05-24 07:09 -0400
      Re: ArrayAccess interface, Traversable interface and foreach Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-24 14:37 +0200
    Re: ArrayAccess interface, Traversable interface and foreach Adam Harvey <usenet@adamharvey.name> - 2014-05-27 18:59 +0000
    Re: ArrayAccess interface, Traversable interface and foreach "kurtk@pobox.com" <kurtk@pobox.com> - 2014-06-02 07:15 -0700

csiph-web