34 lines
1.5 KiB
Markdown
34 lines
1.5 KiB
Markdown
---
|
|
title: Dependency-free PHP Collections
|
|
date: 2025-03-29
|
|
permalink: daily/2025/03/29/collections
|
|
tags:
|
|
- software-development
|
|
- php
|
|
- collections
|
|
cta: ~
|
|
snippet: |
|
|
How to write Collection classes in PHP with no dependencies.
|
|
---
|
|
|
|
A few months ago, [Beyond Blocks podcast guest Dan Leech][5] give another talk at the PHP South West user group.
|
|
|
|
This talk was about [building an expression language from scratch][4], but in one part of the talk, Dan showed how he creates Collection classes.
|
|
|
|
I use Collection classes a lot, have [given talks about them][1] and [wrote a Collections module for Drupal 7][2].
|
|
|
|
In Dan's talk, instead of using a Collection from Laravel or Doctrine, [he created his own][3].
|
|
|
|
His extended PHP's `IteratorAggregate` class so had no external dependencies.
|
|
|
|
He could then add whatever additional methods and functionality he needed.
|
|
|
|
Looking at other repositories on GitHub, I was able to find other examples.
|
|
|
|
I've done this on projects since Dan's talk and like taking the minimalist approach - avoiding adding a dependency to my project and only adding the functionality I need and functionality that's more specific to my domain.
|
|
|
|
[1]: {{site.url}}/presentations/using-illuminate-collections-outside-laravel
|
|
[2]: https://www.drupal.org/project/collection_class
|
|
[3]: https://github.com/dantleech/onehourexpr/blob/8c4ee4a3c1680455118e16a3e1be2a08418ab207/src/Tokens.php
|
|
[4]: https://www.youtube.com/watch?v=UpyVAcWGQhg
|
|
[5]: {{site.url}}/podcast/6-dan-leech-php-tui
|