I have a trait like this
Code: Select all
trait MethodBuilder
{
protected $methods = array();
public function addMethod($methodName, $callback)
{
// ...
$this->methods[$methodName] = $callback->bindTo($this);
}
public function __call($methodName, array $args)
{
if (isset($this->methods[$methodName])) {
return call_user_func_array($this->methods[$methodName], $args);
}
}
}
[syntax]
class Test{
use MethodBuilder;
}
[/syntax]
Then I do
[syntax]
$obj = new Test;
$obj->name="Sheikh Heera";
$obj->addMethod('getName', function(){
return $this->name . '<br />';
});
echo $obj->getName(); // Sheikh Heera, works
[/syntax]
Then another object
[syntax]
$newObj = new stdClass; // tried without this
$newObj->name = "The Rock";
$newObj = clone $obj;
echo $newObj->getName(); // Sheikh Heera, but I expected "The Rock"
[/syntax]
Is that possible ? Looks like the trait is not getting cloned to the $newObg , Ant work around for that ?
An example here : http://codepad.viper-7.com/NPAs1i.
Thanks for your effort