chillerlan/php-traits
最新稳定版本:3.0.2
Composer 安装命令:
composer require chillerlan/php-traits
包简介
Some useful traits for PHP 7.2+
关键字:
README 文档
README
A collection of (more or less) useful traits for PHP7.2+
Features
ClassLoader- invokes objects of a given class and interface/type with an arbitrary count of constructor argumentsMagic- turns methods into magic propertiesEnumerable- provides some of prototype's enumerable methods, implementsEnumerableInterfaceArrayHelpersByteArray- useful for byte/bit-flipping purposes, extendsSplFixedArrayByteArrayDispenser- createsByteArrayfrom several data types (hex, base64, binary, json etc.)DotArray- adds dot key notation functionalitySearchableArray- deep search arrays usingRecursiveIteratorIterator
InterfacesArrayAccessTrait- implementsArrayAccessIteratorTrait- implementsIterator
SPLCountableTrait- implementsCountableSeekableIteratorTrait- implementsSeekableIterator
Documentation
Installation
requires composer
composer.json (note: replace dev-master with a version boundary)
{
"require": {
"php": "^7.2",
"chillerlan/php-traits": "dev-master"
}
}
Manual installation
Download the desired version of the package from master or release and extract the contents to your project folder. After that:
- run
composer installto install the required dependencies and generate/vendor/autoload.php. - if you use a custom autoloader, point the namespace
chillerlan\Traitsto the foldersrcof the package
Profit!
Usage
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 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
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;
chillerlan/php-traits 适用场景与选型建议
chillerlan/php-traits 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.21M 次下载、GitHub Stars 达 16, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「trait」 「helper」 「dotenv」 「PHP7」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chillerlan/php-traits 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chillerlan/php-traits 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chillerlan/php-traits 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Datetime helpers for timezone handling
Trait providing methods to set class properties with an array.
A fast and intuitive dependency injection container.
Library with classes to help you do batch.
Expansion traits for Yii2 components
Traits to build collections of specific objects
统计信息
- 总下载量: 1.21M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 32
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04