views:

278

answers:

2

I'm learning PHP5 (last time I checked PHP was in PHP4 days) and I'm glad to see that PHP5 OO is more Java-alike than the PHP4 one but there's still an issue that makes me feel quite unconfortable because of my Java background : ARRAYS.

I'm reading "Proffesional PHP6" (Wrox) and It shows its own Collection implementation. I've found other clases like the one in http://aheimlich.dreamhosters.com/generic-collections/Collection.phps based on SPL. I've also found that there's some kind of Collection in SPL (ArrayObject)

However, I'm surprised because I don't really see people using Collections in PHP, they seem to prefer arrays.

So, ¿Isn't it a good idea using Collections in PHP just like people use ArrayList instead of basic arrays in Java? (In fact, php arrays aren't really like java arrays).

+3  A: 

Collections in Java make a lot of sense since it's a strongly typed language. It makes sense to have a collection of say "Cars" and another of "Motorbikes".

However, in PHP, due to the dynamically typed nature, it is quite common to sacrifice the formality of Collections. Arrays are suffice to be used as generic containers of various object types (Cars, Motorbikes, etc.). Also, the added benefit comes from the fact that the arrays can be mutated very easily (which sometimes can be a big disadvantage when proper error checking is absent).

I come from a Java background, and I've found that using a Collections design pattern in PHP does not buy much in the way of advantages (no multi-threading, no optimization of memory allocation, no iterators, etc.).

If you're looking for any of those advantages, its probably better to construct a wrapper class around the array, implementing each feature (iterators, etc.) a la carte.

Pras
Constructing my own wrapper class has the disadvantage of using a non standart class for such a basic thing. Is SPL's ArrayObject a wrapper?
ktulur
Agreed, any custom wrappers have the disadvantage of being non-standard, but that may not be so bad since there really isn't a standard Collections wrapper.I haven't really worked with SPL's ArrayObject, but looking at the docs (http://www.php.net/~helly/php/ext/spl/classArrayObject.html), it seems to be a wrapper class that adds a clone function and iterators. If this is all you ever need, I'd recommend using it. But if you need synchronization, linkedlist vs. arraylist, etc. you'll probably need to either write your own wrapper or extend ArrayObject.
Pras
+3  A: 

PHP arrays are associative... They're far more powerful than Java's arrays, and include much of the functionality of List<> and Map<>.

What do you mean by "good idea"? They're different tools, using one language in the way you used another usually results in frustration.

Nick Veys
There're a lot of OO design patterns which are being used in PHP and the PHP5 OO is really Java-alike so using some kind of Collection seemed a good idea to me.
ktulur
The fact that PHP borrowed some things from Java is not necessarily a good thing.
Ionuț G. Stan
I guess I'm confused. You _do_ have "collections". They're just not named "List" or "Map". The PHP arrays will grow dynamically. You can associate key/value pairs.Sets are a little different beast, but hey, can't pile too much in there. :) Coming from Java it's certainly a strange world out there, but embrace these differences and the tradeoffs that come with them!
Nick Veys