dev-master
9999999-dev http://github.com/ulrichsg/php-collectionsCollection classes for PHP 5.3
MIT
The Requires
- php >=5.3.0
The Development Requires
by Ulrich Schmidt-Goertz
Collection classes for PHP 5.3
PHP's arrays are a jack-of-all-trades data structure - highly flexible, but there are numerous use cases where I would rather have more specific functionality like the one offered by Java's Collection API. Since PHP 5, the Standard PHP Library offers classes that cover some of these cases, but not nearly all. This project is an attempt to add more., (*1)
Imagine the following bit of code that aggregates values, indexed by type:, (*2)
$totals = array(); foreach ($items as $item) { if (!isset($totals[$item->type])) { $totals[$item->type] = 0; } $totals[$item->type] += $item->value; }
The if (!isset(...))
block makes this piece of code rather ugly, but it is necessary to avoid even uglier warnings.
Using the NumberMap
class from this package we can avoid it and just write:, (*3)
$totals = new NumberMap(); foreach ($items as $item) { $totals->add($item->type, $item->value); }
So far the package only provides two collection types:
* Map
- basic key-value map
* NumberMap
- a map that can do some math on its entries (see the example above), (*4)
I'd love to have more, so send me your ideas - or your pull requests :), (*5)
For maximum convenience, all classes in this package implement the following interfaces: * ArrayAccess - square-bracket syntax * IteratorAggregate - foreach loops * Countable - count() support, (*6)
ulrichsg/php-collections
to your composer.json.make
to see a few handy commands.Go here for PHPDoc-generated documentation., (*7)
This package is released under the MIT license., (*8)
Collection classes for PHP 5.3
MIT