Refactor Class properties of type Array to use collection classes instead.
-----------
Objects often contain groups of related objects
Categories, tags, comments, reviews, anywhere there are data joins etc.
Take a relatively straightforward real world item such as a music Album..
Artist Creates Albums, Albums Have Songs, Could be tagged, have reviews & ratings.
Normally we use arrays for this but we can do better
An array is just big empty bucket… it doesn’t provide context, it doesn’t have any methods or properties - just members.
We need to add extra validation to ensure our arrays store the same types of information
No rules on how items are added, since Arrays can be key=>value or numeric index, can even be a mix of keyed and non keyed. can be nested An Array is just a big bucket we can throw stuff in
Arrays are very easy to use, we all know
- how to dump something in an array
- how to access a value by key
- how to loop through an array, split and array, pop values off
That long legacy of array functions to help work with arrays.
- maybe that’s a “what’s wrong with arrays” too many array_ functions.
So what is a collection class?
Yes.. it’s still a big old bucket to dump stuff in…
So It’s still an array at it’s core.. *but it’s more than just an array
*
The array members are intrinsically linked to our objects, there is only one TYPE of object stored in a collection.
You can apply rules that reject objects of the correct type but which are missing specific values or properties.
This Separation of objects and encapsulation with rules makes testing much easier.
Controlling indexes.. whereas arrays could be numeric, text keys our keys could be more stringently controlled, however more common to not have any keys (so numeric then!)
*non member properties* This is arguably the biggest benefit.
A plain array is just the array members, anything else e.g. aggregate functions such as SUM, AVG MIN MAX etc, boolean states, labels, tags, diffs etc are all outside our array and require an extra class and extra layer of containment.
Our collection class can have additional properties or methods
e.g.
$troopers = new ArmyCollection()->isGoodGuys = false
$rebels = new ArmyCollection()->isGoodGuys = true
A Mutable object can be changed
you could add elements, edit existing elements or remove elements
Immutable elements cannot be changed. you can create an object, you can delete it and recreate a slightly different new object. Immutable collection classes don’t need to have accessor methods to add, get, change or remove individual elements.
You don’t need to worry about indexes either.
There are no direct access methods for specific collection members.
We add items when we construct the object
with no addItem or setItems() methods, the object can only ever be read or destroyed - it cannot be altered.
What’s the point in that? Lightweight class that offers Strict Type checked members, no mutability means members can’t transform into something unexpected.
to work with the members remove items etc, transform to an Array with a __toArray() method
work away and then create a new Collection.