2017 © Pedro Peláez
 

library php-collections

Collection classes for PHP 5.3

image

ulrichsg/php-collections

Collection classes for PHP 5.3

  • Wednesday, July 17, 2013
  • by ulrichsg
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Collections Library for PHP

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)

What? Why?

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);
}

Collection types

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)

Common features

For maximum convenience, all classes in this package implement the following interfaces: * ArrayAccess - square-bracket syntax * IteratorAggregate - foreach loops * Countable - count() support, (*6)

Installation

  • As a project dependency: Just add ulrichsg/php-collections to your composer.json.
  • Stand-alone for hacking: Clone this repository and run make to see a few handy commands.

API documentation

Go here for PHPDoc-generated documentation., (*7)

License

This package is released under the MIT license., (*8)

The Versions

17/07 2013

dev-master

9999999-dev http://github.com/ulrichsg/php-collections

Collection classes for PHP 5.3

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Ulrich Schmidt-Goertz