PHPackages                             hansel23/generic-lists - 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. hansel23/generic-lists

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

hansel23/generic-lists
======================

Generic list with interface. Methods are based on the list types of some other famous languages, like java or c#

v1.1.2(10y ago)03.4kMITPHPPHP &gt;=5.5

Since May 17Pushed 10y ago1 watchersCompare

[ Source](https://github.com/Hansel23/GenericLists)[ Packagist](https://packagist.org/packages/hansel23/generic-lists)[ RSS](/packages/hansel23-generic-lists/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (9)Dependencies (2)Versions (11)Used By (0)

[![Build Status](https://camo.githubusercontent.com/7d8ea9581e4c55343e0a0c630af47c6b149fca573074fcde45ca5312fad17a4f/68747470733a2f2f7472617669732d63692e6f72672f48616e73656c32332f47656e657269634c697374732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Hansel23/GenericLists)[![Coverage Status](https://camo.githubusercontent.com/3930f5ff44b6a97822fa74036f7851f5354784161b0a69e17c0ce53eddfc0d9e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f48616e73656c32332f47656e657269634c697374732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Hansel23/GenericLists?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/49c2f99e2ec898858aace2b6e05c630a181901d8a29bc67242dedaf8d1ac35f5/68747470733a2f2f706f7365722e707567782e6f72672f68616e73656c32332f67656e657269632d6c697374732f762f737461626c65)](https://packagist.org/packages/hansel23/generic-lists)[![Latest Unstable Version](https://camo.githubusercontent.com/f4fe10870be04e31ac67e355198f700916b8f2447b3a0eadc6cbd1999b20be14/68747470733a2f2f706f7365722e707567782e6f72672f68616e73656c32332f67656e657269632d6c697374732f762f756e737461626c65)](https://packagist.org/packages/hansel23/generic-lists)[![Total Downloads](https://camo.githubusercontent.com/9fb581f5343ee9ef18a22873cd064d4449e11a65eead268201e01c0d937084cb/68747470733a2f2f706f7365722e707567782e6f72672f68616e73656c32332f67656e657269632d6c697374732f646f776e6c6f616473)](https://packagist.org/packages/hansel23/generic-lists)[![License](https://camo.githubusercontent.com/8811073bfbb377fbdbc47b9ab09b708497031258a0bc1839a50e87417e3e3972/68747470733a2f2f706f7365722e707567782e6f72672f68616e73656c32332f67656e657269632d6c697374732f6c6963656e7365)](https://packagist.org/packages/hansel23/generic-lists)

GenericList
===========

[](#genericlist)

Generic list with interface. It's based on the list type of some other famous languages, like java or c#

### My Motivation

[](#my-motivation)

I like strong typing. I think it makes the code more stable and easier to understand.

I want to be sure to have certain objects in the list and not only trust to have the right object in the array.

I like the methods of the list types of other languages like java or c#. They are often usefull.

### Usage

[](#usage)

You can build an instance of the GenericList on the fly: *new GenericList( YourOwnType::class )*

*Recommended usage:*

1. create a new type, e.g. YourOwnTypeList
2. extend this type from **GenericList**
3. override the constructor: *public function \_\_construct() { parent::\_\_construct( YourOwnType::class ) }*

Now you can use typehints for this list.

You can iterate over the list like you do it with a normal array.

Also there are many useful methods:

- **add( $item )** Adds a new item to the list
- **addAll( ListsItems $list )** Adds another list to the items. No item will be overwritten
- **merge( ListsItems $list )** Merges lhe list with another list. If the index already exists, the item will be overwritten with the new one.
- **remove( $item )** Removes an item from the list. Only the first item that is equal to the given item will be removed.
- **removeByIndex( $index )** Removes the item with the given index. If the index doesn't exist an InvalidIndexException will be thrown.
- **removeAll( ListsItems $list )** Removes all items from another list from the current list.
- **removeAllExcept( ListsItems $list )** Removes all items from the list except the items of the given list.
- **contains( $item )** Looks if the given item exists in the list.
- **indexOf( $item )** Returns the index of the first item that equals to the given item. If there is no equal item in the list, the method returns -1.
- **lastIndexOf( $item )** Returns the index of the last item that equals to the given item. If there is no equal item in the list, the method returns -1.
- **set( $index, $item )** Overwrites the item with the given index in the list. This method is equal to $list\[$index\] = $item, except that the index must be an integer and already existing.
- **get( $index )** Returns the item with the given index. If the index doesn't exist, an InvalidIndexException will be thrown.
- **toArray()** Returns an array-representation of the list, so you can use array functions.
- **reverse()** Reverses the whole list.
- **clear()** Clears the list. All items will be removed and the indices resetted.
- **sortBy( SortsLists $sorter )** Sorts the list by a list sorter.
- **find( FindsItems $filter )** Returns the first item that will be found by your list filter.
- **findLast( FindsItems $filter )** Returns the last item that will be found by your list filter.
- **findAll( FindsItems $filter )** Returns a list of all items that will be found by your list filter.
- **getItemType()** Returns the type of the items in the list.

All exceptions are extended from the ListException.

### List Sorters

[](#list-sorters)

To sort a list, you have to create a sorter, that implements the **SortsLists** interface.

The implementation of the method *compare( $object1, $object2 )* must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Pass an instance of your sorter implementation to the method sortBy of the list.

### List Filters

[](#list-filters)

To filter a list, you have to create a filter, that implements the **FindsItems** interface with the *isValid( $object )*method. This method must return a boolean value to indicate whether the object matches to the filter and should be returned or not.

Pass an instance of your filter implementation to the method *find*, *findLast* or *findAll* of the list. *Find* and *findLast* will return one object (first or last) that matches your filter method and *findAll* will return a new list of the same type that contains all objects filtered by your filter.

### Example

[](#example)

*using list type directly*

```

```

*creating the list type*

```

```

*creating a sorter*

```

```

*using typehint for the list and sort with the created sorter*

```

```

*creating a filter*

```

```

*using the filter*

```

```

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity64

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

9

Last Release

3668d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8b4875ebadf24ddeae09bd59c44c9a86a61c3d3892fe1d4a264ad167b6d230cc?d=identicon)[Hansel23](/maintainers/Hansel23)

###  Code Quality

TestsCodeception

### Embed Badge

![Health badge](/badges/hansel23-generic-lists/health.svg)

```
[![Health](https://phpackages.com/badges/hansel23-generic-lists/health.svg)](https://phpackages.com/packages/hansel23-generic-lists)
```

###  Alternatives

[phpmath/biginteger

A PHP library to work with big integers.

246.1k1](/packages/phpmath-biginteger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
