damijanc/collection

A simple PHP collection class to be used instead of ArrayObject

1.0.0 2018-10-09 13:52 UTC

This package is auto-updated.

Last update: 2024-04-10 03:54:06 UTC


README

A simple PHP collection class to be used instead of ArrayObject

Instead of using ArrayObject you can use simple collection. It implements ArrayAccess and IteratorAggregate so you can use it as an array:

$myCollection = new Collection;
$myCollection[] = 'value1';
$myCollection[] = 'value2';
$myCollection['data'] = 'value3';

and you can iterate it

foreach ($myCollection as $key => $item) {
  //you logic here
}

and you can use it as a base for your cool stuff like type hinting

class MyCoolCollection extends Collection
{
  
    public function add(MyCoolInterface $myCoolThing)
    {
        $this->collectionItems[] = $myCoolThing;
        return $this;
    }

    public function offsetGet($offset): MyCoolInterface
    {
        return parent::offsetGet($offset);
    }
}

Installation:

composer require damijanc/collection