PHPackages                             chillerlan/php-traits - 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. chillerlan/php-traits

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

chillerlan/php-traits
=====================

Some useful traits for PHP 7.2+

3.0.2(7y ago)191.3M↓42.9%[1 issues](https://github.com/chillerlan/php-traits/issues)4MITPHPPHP ^7.2

Since Nov 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/chillerlan/php-traits)[ Packagist](https://packagist.org/packages/chillerlan/php-traits)[ Docs](https://github.com/chillerlan/php-traits)[ RSS](/packages/chillerlan-php-traits/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (1)Versions (22)Used By (4)

chillerlan/php-traits
=====================

[](#chillerlanphp-traits)

A collection of (more or less) useful traits for PHP7.2+

[![version](https://camo.githubusercontent.com/36afdc49d217a5e76838abdfcd39251689363bfb27ebc4cf1f098f86c26ed56d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6368696c6c65726c616e2f7068702d7472616974732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/chillerlan/php-traits)[![license](https://camo.githubusercontent.com/45def4fc29fff5ea3677c73d7c84de7e4937a6bd284516d0463382d4840a891c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6368696c6c65726c616e2f7068702d7472616974732e7376673f7374796c653d666c61742d737175617265)](https://github.com/chillerlan/php-traits/blob/master/LICENSE)[![Travis](https://camo.githubusercontent.com/b16d37e3324e559e9872a7a27b17eb1c14f1042ccb2aa189c73dbcde34bd7026/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6368696c6c65726c616e2f7068702d7472616974732e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/chillerlan/php-traits)[![Coverage](https://camo.githubusercontent.com/7981295c65c2f4a8c9f27513c6152f81e375492766adb828210c15ac0c29ecc5/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6368696c6c65726c616e2f7068702d7472616974732e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/github/chillerlan/php-traits)[![Scrunitizer](https://camo.githubusercontent.com/728d2dda7bca18cdf73b8c76e93b98936ff6a0068fe22a226d79a811d1448564/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6368696c6c65726c616e2f7068702d7472616974732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/chillerlan/php-traits)[![Packagist downloads](https://camo.githubusercontent.com/c8e16a90d8ad0d3bab5a448579318c7be80dcf7cd7c02493cfa21e8e39a2c725/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6368696c6c65726c616e2f7068702d7472616974732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/chillerlan/php-traits/stats)[![PayPal donate](https://camo.githubusercontent.com/1898364bc025197736eadf98f27b48ed82c6c63b9ba33e201eaae5cbde03ae42/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f6e6174652d70617970616c2d6666333361612e7376673f7374796c653d666c61742d737175617265)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=WLYUNAT9ZTJZ4)

Features
--------

[](#features)

- `ClassLoader` - invokes objects of a given class and interface/type with an arbitrary count of constructor arguments
- `Magic` - turns methods into magic properties
- `Enumerable` - provides some of [prototype's enumerable methods](http://api.prototypejs.org/language/Enumerable/), implements `EnumerableInterface`
- `ArrayHelpers`
    - `ByteArray` - useful for byte/bit-flipping purposes, extends [`SplFixedArray`](http://php.net/manual/class.splfixedarray.php)
    - `ByteArrayDispenser` - creates `ByteArray` from several data types (hex, base64, binary, json etc.)
    - `DotArray` - adds dot key notation functionality
    - `SearchableArray` - deep search arrays using [`RecursiveIteratorIterator`](http://php.net/manual/class.recursiveiteratoriterator.php)
- `Interfaces`
    - `ArrayAccessTrait` - implements [`ArrayAccess`](http://php.net/manual/class.arrayaccess.php)
    - `IteratorTrait` - implements [`Iterator`](http://php.net/manual/class.iterator.php)
- `SPL`
    - `CountableTrait` - implements [`Countable`](http://php.net/manual/class.countable.php)
    - `SeekableIteratorTrait` - implements [`SeekableIterator`](http://php.net/manual/class.seekableiterator.php)

Documentation
-------------

[](#documentation)

### Installation

[](#installation)

**requires [composer](https://getcomposer.org)**

*composer.json* (note: replace `dev-master` with a version boundary)

```
{
	"require": {
		"php": "^7.2",
		"chillerlan/php-traits": "dev-master"
	}
}
```

#### Manual installation

[](#manual-installation)

Download the desired version of the package from [master](https://github.com/chillerlan/php-traits/archive/master.zip) or [release](https://github.com/chillerlan/php-traits/releases) and extract the contents to your project folder. After that:

- run `composer install` to install the required dependencies and generate `/vendor/autoload.php`.
- if you use a custom autoloader, point the namespace `chillerlan\Traits` to the folder `src` of the package

Profit!

### Usage

[](#usage)

#### `ClassLoader`

[](#classloader)

Simple usage:

```
class MyClass{
	use ClassLoader;

	protected function doStuff(string $class){
		$obj = $this->loadClass(__NAMESPACE__.'\\Whatever\\'.$class);

		// do stuff
	}
}
```

Let's assume we have several classes that implement the same interface, but their constructors have different parameter counts, like so:

```
class SomeClass implements MyInterface{
	public funtion __construct($param_foo){}
}

class OtherClass implements MyInterface{
	public funtion __construct($param_foo, $param_bar){}
}
```

Initialize an object based on a selction

```
class MyClass{
	use ClassLoader;

	protected $classes = [
		'foo' => SomeClass::class,
		'bar' => OtherClass::class
	];

	protected funtion initInterface(string $whatever, $foo, $bar = null):MyInterface{

		foreach($this->classes as $what => $class){
			if($whatever === $what){
				return $this->loadClass($class, MyInterface::class, $foo, $bar);
			}
		}

	}
}
```

#### `Magic`

[](#magic)

`Magic` allows to access internal methods like as properties.

```
class MyMagicContainer{
	use Magic;

	protected $foo;

	protected function magic_get_foo(){
		// do whatever...

		return 'foo: '.$this->foo;
	}

	protected function magic_set_foo($value){
		// do stuff with $value
		// ...

		$this->foo = $value.'bar';
	}
}
```

```
$magic = new MyMagicContainer;

$magic->foo = 'foo';

var_dump($magic->foo); // -> foo: foobar
```

#### `Enumerable`

[](#enumerable)

```
class MyEnumerableContainer implements EnumerableInterface{
	use Enumerable;

	public function __construct(array $data){
		$this->array = $data;
	}
}
```

```
$enum = new MyEnumerableContainer($data);

$enum
	->__each(function($value, $index){
		// do stuff

		$this->array[$index] = $stuff;
	})
	->__reverse()
	->__to_array()
;

$arr = $enum->__map(function($value, $index){
	// do stuff

	return $stuff;
});

$enum;
```

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~24 days

Recently: every ~68 days

Total

21

Last Release

2638d ago

Major Versions

1.1.13 → 2.0.02018-08-08

2.0.0 → 3.0.02018-09-10

PHP version history (3 changes)1.0.0PHP &gt;=7.0.3

2.0.0PHP &gt;=7.2.0

3.0.1PHP ^7.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/592497?v=4)[smiley](/maintainers/codemasher)[@codemasher](https://github.com/codemasher)

---

Top Contributors

[![codemasher](https://avatars.githubusercontent.com/u/592497?v=4)](https://github.com/codemasher "codemasher (92 commits)")

---

Tags

arraydotenvhelpermagicphp-spl-iteratorsphp7traitcontainerhelpertraitdotenvPHP7

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/chillerlan-php-traits/health.svg)

```
[![Health](https://phpackages.com/badges/chillerlan-php-traits/health.svg)](https://phpackages.com/packages/chillerlan-php-traits)
```

###  Alternatives

[chillerlan/php-settings-container

A container class for immutable settings objects. Not a DI container.

3432.2M22](/packages/chillerlan-php-settings-container)[markbaker/enumhelper

A small library that provides Helper Traits for PHP 8.1 Enums

2821.0k1](/packages/markbaker-enumhelper)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2518.6k](/packages/iteks-laravel-enum)

PHPackages © 2026

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