PHPackages                             jtl/php-generic-collection - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jtl/php-generic-collection

ActiveJtl-library[Utility &amp; Helpers](/categories/utility)

jtl/php-generic-collection
==========================

An implementation of a generic collection for PHP

1.0.1(8mo ago)264.2k—6.3%1[6 PRs](https://github.com/jtl-software/php-generic-collection/pulls)4MITPHPCI passing

Since May 21Pushed 5mo ago3 watchersCompare

[ Source](https://github.com/jtl-software/php-generic-collection)[ Packagist](https://packagist.org/packages/jtl/php-generic-collection)[ RSS](/packages/jtl-php-generic-collection/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (32)Used By (4)

Generic Collection
==================

[](#generic-collection)

[![Build status](https://camo.githubusercontent.com/5f5f62c05933d7cdf4bc7328447b8465edb0a6722d8058173f939334566bee7f/68747470733a2f2f7472617669732d63692e636f6d2f6a746c2d736f6674776172652f7068702d67656e657269632d636f6c6c656374696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/jtl-software/php-generic-collection)

An implementation of generic collections in PHP using iterators. Brings type safety to your arrays without hacks or ugly code.

Usage
-----

[](#usage)

The primary way of using this library is to create your own collection class which extends the `GenericCollection` and sets its type in the constructor like so:

```
use JTL\Generic\GenericCollection;

class MyItemCollection extends GenericCollection {
    public function __construct()
    {
        parent::__construct(MyItem::class);
    }
}
```

You can now add new items to your collection in bulk, using `$collection[] = ...` or by using the static `from` function.

```
$collection = new MyItemCollection();
$item1 = new MyItem(1);
$item2 = new MyItem(2);
$item3 = new MyItem(3);
$itemList = [
    $item1,
    $item2,
    $item3
];

$collection[] = $item1;
$collection->addItemList([$item2, $item3]);

$collection2 = MyItemCollection::from($item1, $item2, $item3);
$collection3 = MyItemCollection::from(...$itemList);
```

Alternative you can just use the generic `ObjectCollection` without creating a new collection class for every object type.

```
$collection = new ObjectCollection(MyItem::class);
$item1 = new MyItem(1);
$item2 = new MyItem(2);
$item3 = new MyItem(3);
$itemList = [
    $item1,
    $item2,
    $item3
];

$collection[] = $item1;
$collection->addItemList([$item2, $item3]);

$collection2 = ObjectCollection::from($item1, $item2, $item3);
$collection3 = ObjectCollection::from(...$itemList);
```

Trying to add a new item that's not of the specified type will throw an InvalidArgumentException.

```
$collection = new ObjectCollection(MyItem::class);
$item1 = new MyItem(1);
$item2 = 'not MyItem';
$item3 = new MyItem(3);

$collection[] = $item1;
$collection[] = $item2; //
