PHPackages                             thakladd/phenerics - 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. thakladd/phenerics

ActiveLibrary

thakladd/phenerics
==================

A package to create some kind of generics in PHP.

00PHP

Since Mar 7Pushed 3y ago1 watchersCompare

[ Source](https://github.com/ThaKladd/Phenerics)[ Packagist](https://packagist.org/packages/thakladd/phenerics)[ RSS](/packages/thakladd-phenerics/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Phenerics
=========

[](#phenerics)

What I try to do with this small experiment is to make something similar to Generics, but in PHP, that does not use docblocks for it, nor monomorphization. It does use eval() to create the new named class that extends the core Collection object, and this does not work well with any IDE, but if that does not stop you, then try it out. This is meant more of a fun concept test than an actual production ready package.

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

[](#installation)

Needs:

- PHP &gt;= 8.1
- Composer

```
composer require thakladd/phenerics
```

Usage
-----

[](#usage)

For all types the value gotten from [gettype](https://www.php.net/manual/en/function.gettype.php) will be used, except it the type is an object, then the result of [get\_class](https://www.php.net/manual/en/function.get-class.php) will be used(unless the type you want in the collection is "object").

The collection object returned is a wrapper for the array, but works as an array because it implements [Iterator](https://www.php.net/manual/en/class.iterator.php), [ArrayAccess](https://www.php.net/manual/en/class.arrayaccess.php), and [Countable](https://www.php.net/manual/en/class.countable.php).

When building an Collection you have to use the function called **arrayOf** where the argument is the type. The function returns an empty Collection for that type called **ArrayOfType**, and you can all that object in order to fill it with an array of that type, and the Collection will do the checks, and throw and exception if there is a mismatch in type.

On a sidenote, I'm a bit on the fence on calling it ArrayOfType, but there is really no good alternative as how I would like to call it(Array&lt;Type&gt;), is not possible. I was considering Array«Type» but those characters are not too accessible on the keyboard. Alternative could be Array\_Type but I don't like the snake-camel casing of it. A simple ArrayType could work as well, even if it does not look well with scalar types in PHP like Arrayinteger even if ***class names are case insensitive***, mixing small i here and big I there is not ideal.

Examples
--------

[](#examples)

Here is an example with a simple array of integers.

```
//Note the type hint here
function  printIntList(ArrayOfInteger  $list) {
    print_r($list->getList());
}

//Type hint comes from here
$array = arrayOf('integer')([1, 2, 3, 4, 5]);
printIntList($array); //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
print_r($array[1]); //2
$array[] = 6;
print_r($array[5]); //6
print_r($array->get(2)); //3
foreach ($array as $key => $value) {
    echo  $key  .  ': '  .  $value . ', '; //0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6,
}
```

And here one with objects.

```
class  Test {
    public  function  __construct(public  string  $x = '') {}
}

function  printTestList(ArrayOfTest  $list) {
    print_r($list->getList());
}

$arrayOfTest = arrayOf('Test')([new  Test('Hello'), new  Test('On'), new  Test('You')]);
$arrayOfTest->add(3, new  Test('You'));
$arrayOfTest[] = new  Test('Old');
$arrayOfTest['assoc'] = new  Test('Glue');
printTestList($arrayOfTest); //Array ( [0] => Test Object ( [x] => Hello ) [1] => Test Object ( [x] => On ) [2] => Test Object ( [x] => You ) [3] => Test Object ( [x] => You ) [4] => Test Object ( [x] => Old ) [assoc] => Test Object ( [x] => Glue ) )
```

License
-------

[](#license)

[MIT](https://choosealicense.com/licenses/mit/)

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity0

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity23

Early-stage or recently created project

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/370ccff30abcfaa260db70f24bb9bda3a43898adb0b1e3957b904812516b2228?d=identicon)[ThaKladd](/maintainers/ThaKladd)

### Embed Badge

![Health badge](/badges/thakladd-phenerics/health.svg)

```
[![Health](https://phpackages.com/badges/thakladd-phenerics/health.svg)](https://phpackages.com/packages/thakladd-phenerics)
```

PHPackages © 2026

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