Answers
php 不支持动态继承 ... 哪怕是 trait 也要在定义类的时候写死 ...
如果你非要实现这个功能只能找替代的办法 ... 简略的写了一下 ... 如下 ...
<?php
$foo = new foo( new bar );
echo $foo->sayhi();
class DynamicExtends {
private $object;
protected function setParent( $object ) {
$this->object = $object;
return;
}
public function __call( $name, $parameter ) {
return isset( $this->object ) ?
$this->object->$name( $parameter ) : null;
}
}
class foo extends DynamicExtends {
public function __construct( $object ) {
$this->setParent( $object );
}
}
class bar {
public function sayhi() {
return 'Hi Sunyanzi';
}
}
圣西罗的烟火
answered 11 years, 2 months ago