{"id":15068587,"url":"https://github.com/chillerlan/php-traits","last_synced_at":"2025-10-05T08:30:47.395Z","repository":{"id":60774282,"uuid":"110571724","full_name":"chillerlan/php-traits","owner":"chillerlan","description":"A collection of (more or less) useful traits for PHP7.2+","archived":true,"fork":false,"pushed_at":"2019-03-21T02:38:43.000Z","size":1213,"stargazers_count":18,"open_issues_count":1,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-09-30T13:42:46.041Z","etag":null,"topics":["array","dotenv","helper","magic","php-spl-iterators","php7","trait"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chillerlan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-13T16:20:28.000Z","updated_at":"2023-01-28T08:53:06.000Z","dependencies_parsed_at":"2022-10-04T15:26:17.928Z","dependency_job_id":null,"html_url":"https://github.com/chillerlan/php-traits","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chillerlan%2Fphp-traits","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chillerlan%2Fphp-traits/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chillerlan%2Fphp-traits/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chillerlan%2Fphp-traits/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chillerlan","download_url":"https://codeload.github.com/chillerlan/php-traits/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235378531,"owners_count":18980464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","dotenv","helper","magic","php-spl-iterators","php7","trait"],"created_at":"2024-09-25T01:38:25.968Z","updated_at":"2025-10-05T08:30:42.087Z","avatar_url":"https://github.com/chillerlan.png","language":"PHP","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=WLYUNAT9ZTJZ4"],"categories":[],"sub_categories":[],"readme":"# chillerlan/php-traits\n\nA collection of (more or less) useful traits for PHP7.2+\n  \n[![version][packagist-badge]][packagist]\n[![license][license-badge]][license]\n[![Travis][travis-badge]][travis]\n[![Coverage][coverage-badge]][coverage]\n[![Scrunitizer][scrutinizer-badge]][scrutinizer]\n[![Packagist downloads][downloads-badge]][downloads]\n[![PayPal donate][donate-badge]][donate]\n\n[packagist-badge]: https://img.shields.io/packagist/v/chillerlan/php-traits.svg?style=flat-square\n[packagist]: https://packagist.org/packages/chillerlan/php-traits\n[license-badge]: https://img.shields.io/github/license/chillerlan/php-traits.svg?style=flat-square\n[license]: https://github.com/chillerlan/php-traits/blob/master/LICENSE\n[travis-badge]: https://img.shields.io/travis/chillerlan/php-traits.svg?style=flat-square\n[travis]: https://travis-ci.org/chillerlan/php-traits\n[coverage-badge]: https://img.shields.io/codecov/c/github/chillerlan/php-traits.svg?style=flat-square\n[coverage]: https://codecov.io/github/chillerlan/php-traits\n[scrutinizer-badge]: https://img.shields.io/scrutinizer/g/chillerlan/php-traits.svg?style=flat-square\n[scrutinizer]: https://scrutinizer-ci.com/g/chillerlan/php-traits\n[downloads-badge]: https://img.shields.io/packagist/dt/chillerlan/php-traits.svg?style=flat-square\n[downloads]: https://packagist.org/packages/chillerlan/php-traits/stats\n[donate-badge]: https://img.shields.io/badge/donate-paypal-ff33aa.svg?style=flat-square\n[donate]: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=WLYUNAT9ZTJZ4\n\n## Features\n- `ClassLoader` - invokes objects of a given class and interface/type with an arbitrary count of constructor arguments\n- `Magic` - turns methods into magic properties\n- `Enumerable` - provides some of [prototype's enumerable methods](http://api.prototypejs.org/language/Enumerable/), implements `EnumerableInterface`\n- `ArrayHelpers`\n  - `ByteArray` - useful for byte/bit-flipping purposes, extends [`SplFixedArray`](http://php.net/manual/class.splfixedarray.php)\n  - `ByteArrayDispenser` - creates `ByteArray` from several data types (hex, base64, binary, json etc.)\n  - `DotArray` - adds dot key notation functionality\n  - `SearchableArray` - deep search arrays using [`RecursiveIteratorIterator`](http://php.net/manual/class.recursiveiteratoriterator.php)\n- `Interfaces`\n  - `ArrayAccessTrait` - implements [`ArrayAccess`](http://php.net/manual/class.arrayaccess.php)\n  - `IteratorTrait` - implements [`Iterator`](http://php.net/manual/class.iterator.php)\n- `SPL`\n  - `CountableTrait` - implements [`Countable`](http://php.net/manual/class.countable.php)\n  - `SeekableIteratorTrait` - implements [`SeekableIterator`](http://php.net/manual/class.seekableiterator.php)\n \n## Documentation\n\n### Installation\n**requires [composer](https://getcomposer.org)**\n\n*composer.json* (note: replace `dev-master` with a version boundary)\n```json\n{\n\t\"require\": {\n\t\t\"php\": \"^7.2\",\n\t\t\"chillerlan/php-traits\": \"dev-master\"\n\t}\n}\n```\n\n#### Manual installation\nDownload the desired version of the package from [master](https://github.com/chillerlan/php-traits/archive/master.zip) or \n[release](https://github.com/chillerlan/php-traits/releases) and extract the contents to your project folder.  After that:\n- run `composer install` to install the required dependencies and generate `/vendor/autoload.php`.\n- if you use a custom autoloader, point the namespace `chillerlan\\Traits` to the folder `src` of the package \n\nProfit!\n\n### Usage\n\n#### `ClassLoader`\nSimple usage:\n```php\nclass MyClass{\n\tuse ClassLoader;\n\t\n\tprotected function doStuff(string $class){\n\t\t$obj = $this-\u003eloadClass(__NAMESPACE__.'\\\\Whatever\\\\'.$class);\n\t\t\n\t\t// do stuff\n\t}\n}\n```\n\nLet's assume we have several classes that implement the same interface, but their constructors have different parameter counts, like so:\n```php\nclass SomeClass implements MyInterface{\n\tpublic funtion __construct($param_foo){}\n}\n\nclass OtherClass implements MyInterface{\n\tpublic funtion __construct($param_foo, $param_bar){}\n}\n```\n\nInitialize an object based on a selction\n\n```php\nclass MyClass{\n\tuse ClassLoader;\n\t\n\tprotected $classes = [\n\t\t'foo' =\u003e SomeClass::class, \n\t\t'bar' =\u003e OtherClass::class\n\t];\n\t\n\tprotected funtion initInterface(string $whatever, $foo, $bar = null):MyInterface{\n\t\n\t\tforeach($this-\u003eclasses as $what =\u003e $class){\n\t\t\tif($whatever === $what){\n\t\t\t\treturn $this-\u003eloadClass($class, MyInterface::class, $foo, $bar);\n\t\t\t}\n\t\t}\n\t\n\t}\n}\n```\n\n\n#### `Magic`\n`Magic` allows to access internal methods like as properties.\n```php\nclass MyMagicContainer{\n\tuse Magic;\n\n\tprotected $foo;\n\n\tprotected function magic_get_foo(){\n\t\t// do whatever...\n\t\t\n\t\treturn 'foo: '.$this-\u003efoo;\n\t}\n\n\tprotected function magic_set_foo($value){\n\t\t// do stuff with $value\n\t\t// ...\n\t\t\n\t\t$this-\u003efoo = $value.'bar';\n\t}\n}\n```\n\n```php\n$magic = new MyMagicContainer;\n\n$magic-\u003efoo = 'foo';\n\nvar_dump($magic-\u003efoo); // -\u003e foo: foobar\n\n```\n\n#### `Enumerable`\n```php\nclass MyEnumerableContainer implements EnumerableInterface{\n\tuse Enumerable;\n\n\tpublic function __construct(array $data){\n\t\t$this-\u003earray = $data;\n\t}\n}\n```\n\n```php\n$enum = new MyEnumerableContainer($data);\n\n$enum\n\t-\u003e__each(function($value, $index){\n\t\t// do stuff\n\t\t\n\t\t$this-\u003earray[$index] = $stuff;\n\t})\n\t-\u003e__reverse()\n\t-\u003e__to_array()\n;\n\n$arr = $enum-\u003e__map(function($value, $index){\n\t// do stuff\n\t\n\treturn $stuff;\n});\n\n$enum;\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchillerlan%2Fphp-traits","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchillerlan%2Fphp-traits","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchillerlan%2Fphp-traits/lists"}