dawid-daweb / components
辅助开发模块
dev-master
2015-01-04 12:52 UTC
This package is not auto-updated.
Last update: 2024-09-14 16:29:24 UTC
README
需要 PHP >= 5.4
安装
在你的 composer.json 中
{
require: {
"dawid-daweb/components" "dev-master"
}
}
混入
是向你的对象添加额外属性和方法的一种简单方式。简单示例
<?php use daweb\mixin\MixinTrait; class object { // your object use MixinTrait; // use trait or implemments MixinInterface and create your own logic managment } $object = new Object; $object->mixinAttach('additional_params', [ //attach your additional properties 'prop' => 'Hello ', 'prop2' => 'World', 'callback' => function($name) {return $name;} ]); echo $object->prop; //Hello echo $object->callback('Beautiful'); //Beautiful echo $object->prop2; //World
当我们需要混入某些类时
class MyObject { //for mixin with Object private $prop; public function getProp() { return $this->prop; } } $object->mixinAttach('additional_methods', [//add all public methods to $object 'class' => 'MyObject', //if you need use constructor pass new MyObject 'prop' => 'work' ]); echo $object->getProp2(); //work
混入可以在 mixins() 方法中定义
class object { use MixinTrait; public function mixins() { return [ ['someProp' => 'value'], 'additional_params' => [ 'prop' => 'Hello ', 'prop2' => 'World', 'callback' => function($name) {return $name;} ], 'additional_methods' => [ 'class' => 'MyObject', //if you need use constructor pass new MyObject 'prop' => 'work' ] ] } }
辅助方法
$object->hasMixin('additional_params'); $object->mixinDetach('additional_params'); $object->issetProperty('prop'); //check whether $objec has mixed property