DEV Community

Discussion on: Collection objects in PHP

Collapse
 
aleksikauppila profile image
Aleksi Kauppila • Edited

As you mentioned arrays are very flexible and offer freedom for developers. It's what makes them good but also very annoying to consume. Personally i feel that only arrays containing a single type is acceptable as return value (see my post on the subject). Iterators are better in most cases.

To be honest i really don't think it's important to implement ArrayAccess on collections (or extend ArrayObject). It's not really bad... but somehow using that bracket syntax feels awkward.

Lack of collections is a pain in the ass in PHP. There is really no convenient way to achieve type safety for collections at the moment (at least one i know of). I create collections all the time by just implementing IteratorAggregate but this means that i do have to create a new class each time i want type safety for a collection.

I really hope we'll see generics sooner rather than later. Also sets and maps.