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


Groups > comp.lang.php > #13749 > unrolled thread

ArrayAccess interface, Traversable interface and foreach

Started by"kurtk@pobox.com" <kurtk@pobox.com>
First post2014-05-23 11:22 -0700
Last post2014-06-02 07:15 -0700
Articles 7 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  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

#13749 — ArrayAccess interface, Traversable interface and foreach

From"kurtk@pobox.com" <kurtk@pobox.com>
Date2014-05-23 11:22 -0700
SubjectArrayAccess interface, Traversable interface and foreach
Message-ID<fdd1f0af-6cbf-4577-a4c4-7c9073eae483@googlegroups.com>
The documentation for ArrayAccess (at http://www.php.net/manual/en/class.arrayaccess.php) doesn't show it implementing Traversable. Nevertheless, you can still use any class you write that implements ArrayAccess in a foreach loop. 

This seems like an oversight in the documentation--unless I am missing something?

[toc] | [next] | [standalone]


#13750

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-05-23 16:35 -0400
Message-ID<llobf4$sme$1@dont-email.me>
In reply to#13749
On 5/23/2014 2:22 PM, kurtk@pobox.com wrote:
> The documentation for ArrayAccess (at http://www.php.net/manual/en/class.arrayaccess.php) doesn't show it implementing Traversable. Nevertheless, you can still use any class you write that implements ArrayAccess in a foreach loop.
>
> This seems like an oversight in the documentation--unless I am missing something?
>

Not an oversight, IMHO.  I think it's impossible for the document 
writers to cover *every* possible usage.

That's why there are usage notes, contributed by users like yourself, 
who have found other usages which are valuable (or do not work).

You can always add your own note to the documentation to show how it's 
done; in fact I would encourage it.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13751

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-05-24 12:05 +0200
Message-ID<53806ec6$0$6713$9b4e6d93@newsspool2.arcor-online.net>
In reply to#13749
kurtk@pobox.com wrote:

> The documentation for ArrayAccess (at
> http://www.php.net/manual/en/class.arrayaccess.php) doesn't show it
> implementing Traversable. Nevertheless, you can still use any class
> you write that implements ArrayAccess in a foreach loop.
> 
> 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>.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#13752

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-05-24 07:09 -0400
Message-ID<llpulj$uc5$1@dont-email.me>
In reply to#13751
On 5/24/2014 6:05 AM, Christoph Michael Becker wrote:
> kurtk@pobox.com wrote:
>
>> The documentation for ArrayAccess (at
>> http://www.php.net/manual/en/class.arrayaccess.php) doesn't show it
>> implementing Traversable. Nevertheless, you can still use any class
>> you write that implements ArrayAccess in a foreach loop.
>>
>> 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>.
>

But that is an entirely different access to what Kurt is talking about.

Kurt is discussing how to transverse an array within an object; your 
page discusses how to transverse public data members in an object.  But 
good OO practices says all data members should be private.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#13753

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-05-24 14:37 +0200
Message-ID<5380928f$0$6699$9b4e6d93@newsspool2.arcor-online.net>
In reply to#13752
Jerry Stuckle wrote:

> On 5/24/2014 6:05 AM, Christoph Michael Becker wrote:
>> kurtk@pobox.com wrote:
>>
>>> The documentation for ArrayAccess (at
>>> http://www.php.net/manual/en/class.arrayaccess.php) doesn't show it
>>> implementing Traversable. Nevertheless, you can still use any class
>>> you write that implements ArrayAccess in a foreach loop.
>>>
>>> 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>.
>>
> 
> But that is an entirely different access to what Kurt is talking about.
> 
> Kurt is discussing how to transverse an array within an object; your
> page discusses how to transverse public data members in an object.  But
> good OO practices says all data members should be private.

To traverse an array which is a member of an object $o with foreach($o
...), the object's class has to implement IteratorAggregate or Iterator,
not ArrayAccess.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#13755

FromAdam Harvey <usenet@adamharvey.name>
Date2014-05-27 18:59 +0000
Message-ID<lm2n9m$1i2$1@dont-email.me>
In reply to#13751
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

[toc] | [prev] | [next] | [standalone]


#13778

From"kurtk@pobox.com" <kurtk@pobox.com>
Date2014-06-02 07:15 -0700
Message-ID<c9cd8c8b-9f8b-4d58-ba7d-cc89924cb2e5@googlegroups.com>
In reply to#13751
> 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>.

Thanks Christoph. I didn't realize that any object can be used in a foreach loop, and that it doesn't need to implement an interface that derives from Traversable. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web