承接 chillerlan/php-traits 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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+

version license Travis Coverage Scrunitizer Packagist downloads PayPal donate

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, implements EnumerableInterface
  • ArrayHelpers
    • ByteArray - useful for byte/bit-flipping purposes, extends SplFixedArray
    • ByteArrayDispenser - creates ByteArray from several data types (hex, base64, binary, json etc.)
    • DotArray - adds dot key notation functionality
    • SearchableArray - deep search arrays using RecursiveIteratorIterator
  • Interfaces
  • SPL

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 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

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;

统计信息

  • 总下载量: 1.21M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 3
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固