{ "uuid": [ { "value": "c9ccd7f8-4727-49fa-9481-4507e7986fd9" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-05-11T08:59:58+00:00" } ], "revision_uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "revision_log": [], "status": [ { "value": true } ], "uid": [ { "target_type": "user", "target_uuid": "b8966985-d4b2-42a7-a319-2e94ccfbb849" } ], "title": [ { "value": "Why use Collections?" } ], "created": [ { "value": "2025-03-30T00:00:00+00:00" } ], "changed": [ { "value": "2025-05-11T08:59:58+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2025\/03\/30\/why-collections", "langcode": "en" } ], "body": [ { "value": "\n

Yesterday, I wrote how to create dependency free Collection classes in PHP<\/a> (thanks to Dan Leech).<\/p>\n\n

I said that I've written blog posts<\/a> and given talks<\/a> on using Collection classes.<\/p>\n\n

But why do I like Collections and why may you want to use them instead of native arrays?<\/p>\n\n

The first reason is that I can add extra functionality to Collections, because they're objects.<\/p>\n\n

Whether it's a generic action such as filtering or sorting the items, or something more specific like returning a list of station codes from a collection of train stations, this can be added to specific collection classes.<\/p>\n\n

I'll usually have an AbstractCollection<\/code> that has the generic methods and is extended by specific Collection types with methods more specific methods.<\/p>\n\n

Having specific types of Collection objects also gives my code more context.<\/p>\n\n

Instead of an array that could contain anything, by reading the code and seeing which Collection types are used, I know what the collection contains and what I can do with it.<\/p>\n\n

This is also why I like value objects.<\/p>\n\n

Giving objects specific names instead of relying on the language's primitive types makes the code more robust and easier to read and understand.<\/p>\n\n ", "format": "full_html", "processed": "\n

Yesterday, I wrote how to create dependency free Collection classes in PHP<\/a> (thanks to Dan Leech).<\/p>\n\n

I said that I've written blog posts<\/a> and given talks<\/a> on using Collection classes.<\/p>\n\n

But why do I like Collections and why may you want to use them instead of native arrays?<\/p>\n\n

The first reason is that I can add extra functionality to Collections, because they're objects.<\/p>\n\n

Whether it's a generic action such as filtering or sorting the items, or something more specific like returning a list of station codes from a collection of train stations, this can be added to specific collection classes.<\/p>\n\n

I'll usually have an AbstractCollection<\/code> that has the generic methods and is extended by specific Collection types with methods more specific methods.<\/p>\n\n

Having specific types of Collection objects also gives my code more context.<\/p>\n\n

Instead of an array that could contain anything, by reading the code and seeing which Collection types are used, I know what the collection contains and what I can do with it.<\/p>\n\n

This is also why I like value objects.<\/p>\n\n

Giving objects specific names instead of relying on the language's primitive types makes the code more robust and easier to read and understand.<\/p>\n\n ", "summary": null } ] }