Wallogit.com
2017 © Pedro Peláez
modules for help development
require PHP >= 5.4, (*1)
in your composer.json, (*2)
{
require: {
"dawid-daweb/components" "dev-master"
}
}
is the simple way on add additional properties and methods to your object. Simple example:, (*3)
<?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
When we need mix some class, (*4)
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 can be defined in method mixins(), (*5)
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'
]
]
}
}
Helper methods, (*6)
$object->hasMixin('additional_params');
$object->mixinDetach('additional_params');
$object->issetProperty('prop'); //check whether $objec has mixed property