FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » ArrayAccess interface, Traversable interface and foreach
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
ArrayAccess interface, Traversable interface and foreach [message #185987] Fri, 23 May 2014 18:22 Go to next message
kurtk(at)pobox(dot)com is currently offline  kurtk(at)pobox(dot)com
Messages: 10
Registered: May 2012
Karma: 0
Junior Member
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?
Re: ArrayAccess interface, Traversable interface and foreach [message #185988 is a reply to message #185987] Fri, 23 May 2014 20:35 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/23/2014 2:22 PM, kurtk(at)pobox(dot)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(at)attglobal(dot)net
==================
Re: ArrayAccess interface, Traversable interface and foreach [message #185989 is a reply to message #185987] Sat, 24 May 2014 10:05 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
kurtk(at)pobox(dot)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
Re: ArrayAccess interface, Traversable interface and foreach [message #185990 is a reply to message #185989] Sat, 24 May 2014 11:09 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/24/2014 6:05 AM, Christoph Michael Becker wrote:
> kurtk(at)pobox(dot)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(at)attglobal(dot)net
==================
Re: ArrayAccess interface, Traversable interface and foreach [message #185991 is a reply to message #185990] Sat, 24 May 2014 12:37 Go to previous messageGo to next message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma: 0
Senior Member
Jerry Stuckle wrote:

> On 5/24/2014 6:05 AM, Christoph Michael Becker wrote:
>> kurtk(at)pobox(dot)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
Re: ArrayAccess interface, Traversable interface and foreach [message #185996 is a reply to message #185989] Tue, 27 May 2014 18:59 Go to previous messageGo to next message
Adam Harvey is currently offline  Adam Harvey
Messages: 25
Registered: September 2010
Karma: 0
Junior Member
On Sat, 24 May 2014 12:05:02 +0200, Christoph Michael Becker wrote:

> kurtk(at)pobox(dot)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
Re: ArrayAccess interface, Traversable interface and foreach [message #186028 is a reply to message #185989] Mon, 02 June 2014 14:15 Go to previous message
kurtk(at)pobox(dot)com is currently offline  kurtk(at)pobox(dot)com
Messages: 10
Registered: May 2012
Karma: 0
Junior Member
> 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.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: mysqli fetch_assoc() straight to array
Next Topic: Pthread with amazon sns
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 09:41:14 GMT 2024

Total time taken to generate the page: 0.13919 seconds