PHPackages                             chrisharrison/php-array-of - 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. chrisharrison/php-array-of

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

chrisharrison/php-array-of
==========================

Implement an array of a defined type. Generics replacement for PHP.

1.1(5y ago)11722.4k↓14%22MITPHPPHP ^7.1 || ^8.0

Since Dec 1Pushed 5y ago1 watchersCompare

[ Source](https://github.com/chrisharrison/php-array-of)[ Packagist](https://packagist.org/packages/chrisharrison/php-array-of)[ RSS](/packages/chrisharrison-php-array-of/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (2)

php-array-of
============

[](#php-array-of)

[![Build Status](https://camo.githubusercontent.com/5d936a96a42d74fe822e0fcda927dc2434a788bdea2e41c2378c72d3d746edde/68747470733a2f2f7472617669732d63692e6f72672f63687269736861727269736f6e2f7068702d61727261792d6f662e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/chrisharrison/php-array-of)

Implement an array of a defined type. Generics replacement for PHP.

Requirements
------------

[](#requirements)

Requires PHP &gt;= 7.1

Installation
------------

[](#installation)

Through Composer, obviously:

```
composer require chrisharrison/php-array-of

```

Why?
----

[](#why)

PHP 7 has pretty good support for type declarations, both in arguments and returns. It now also handles primitives as well as class names. For example:

```
public function dealCard(string $cardName): Card;
```

One thing is still lacking. Array generics. There's an [RFC](https://wiki.php.net/rfc/arrayof) for implementing 'Array Of'. It's been around for quite a while and was roundly rejected.

This library is a workaround for that. It allows you to make type declarations for arrays of a particular type.

Usage
-----

[](#usage)

### Using an existing implementation

[](#using-an-existing-implementation)

The library comes with `ArrayOf` implementations for all of the PHP scalar types. i.e.:

- `ArrayOfInteger`
- `ArrayOfFloat`
- `ArrayOfString`
- `ArrayOfBoolean`

These can then be used in a type declaration:

```
public function getIntegers(): ArrayOfInteger;
```

An `ArrayOfInteger` can be created:

```
$integers = new ArrayOfInteger([1,1,2,3,5,8,13]);
```

and used like an array:

```
$sum = $integers[5] + $integers[6]; // equals 21
```

### Implementing your own type

[](#implementing-your-own-type)

You can create your own `ArrayOf`s for your own types.

```
final class ArrayOfCard extends ArrayOf
{
    protected function typeToEnforce(): string
    {
        return Card::class;
    }
}
```

An `ArrayOfCard` can be created thus:

```
$aceOfSpades = new Card('spades', 'ace');
$threeOfClubs = new Card('clubs', '3');

$cards = new ArrayOfCard([$aceOfSpades, $threeOfClubs]);
```

Other concerns
--------------

[](#other-concerns)

### Enforcement

[](#enforcement)

Members of an `ArrayOf` are enforced as being of the type specified in the `typeToEnforce` abstract method. This enforcement occurs on instantiation at runtime. If you try to instantiate with a member of a non-matching type, an exception will be thrown.

### Permissible types

[](#permissible-types)

Only PHP [scalars](http://php.net/manual/en/function.is-scalar.php) and objects can be members of an `ArrayOf`. So no `callable`s and no `array`s.

### Immutability

[](#immutability)

`ArrayOf`s are mutable. This library provides the capability to define immutable objects in the form of `ImmutableArrayOf`s. These objects extend from `ArrayOf` and work in the same way except after the initial instantiation, no further changes can be made to the object. If you try to perform a write operation (e.g. `unset`) on it, an exception will be thrown. All of the scalar types are also provided in immutable form:

- `ImmutableArrayOfInteger`
- `ImmutableArrayOfFloat`
- `ImmutableArrayOfString`
- `ImmutableArrayOfBoolean`

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 92.3% of commits — single point of failure

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 ~1113 days

Total

2

Last Release

1977d ago

PHP version history (2 changes)1.0PHP ^7.1

1.1PHP ^7.1 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/296186?v=4)[Chris Harrison](/maintainers/chrisharrison)[@chrisharrison](https://github.com/chrisharrison)

---

Top Contributors

[![chrisharrison](https://avatars.githubusercontent.com/u/296186?v=4)](https://github.com/chrisharrison "chrisharrison (12 commits)")[![Chemaclass](https://avatars.githubusercontent.com/u/5256287?v=4)](https://github.com/Chemaclass "Chemaclass (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/chrisharrison-php-array-of/health.svg)

```
[![Health](https://phpackages.com/badges/chrisharrison-php-array-of/health.svg)](https://phpackages.com/packages/chrisharrison-php-array-of)
```

PHPackages © 2026

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