chenqd / mixin
此包的最新版本(0.3.1)没有可用的许可证信息。
mixin for php
0.3.1
2017-04-11 01:58 UTC
Requires
- php: >=5.5.9
- illuminate/container: 5.0 - 5.3 || >=5.4.16
- illuminate/support: >=5
This package is not auto-updated.
Last update: 2024-09-29 02:51:58 UTC
README
#mixin 这是一个php的简单mixin实现,参考yii中behavior中的实现其实没能避免多继承的缺陷: 优先顺序模糊、继承方法不明确。 实际上在技术实现上并没有太大的难点,这里提供的就是通过别名来给mixin赋值并通过别名进行调用。
为开发便利引入了一些laravel的组件,感谢laravel作者的优良设计
使用
主类
use chenqd\mixin\HasMixin; class User { use HasMixin; public $name ='shoal'; public function mixinMap() { return [ 'cook' => Cook::class, 'aa'=> [ 'bb' => Cook::class, ], 'aa.cc'=> [ Cook2::class, 'food'=>'大饼' ], ]; } }
mixin类
use chenqd\mixin\MixinTrait; class Cook { use MixinTrait; public function done(){ echo $this->name . " cook a lot of delicious food!\n"; } } class Cook2 { use MixinTrait; public $food; public function __construct($entity, $food){ $this->entity = $entity; $this->food = $food; } public function done(){ //do something echo $this->name . ' cook a '.$this->food."!\n"; //do something } }
调用
$user = new User(); $user->mixinCall('cook')->done(); $user->mixinCall('aa.bb', 'done'); $user->mixinCall('aa.cc', 'done');