PHP中的多重继承模拟,如何优化代码
我有两个类别A和B。类别C可能是A和B。我需要以最佳方式进行PHP中的多重继承模拟,如何优化代码,php,class,multiple-inheritance,extend,Php,Class,Multiple Inheritance,Extend,我有两个类别A和B。类别C可能是A和B。我需要以最佳方式进行 class A { public function testA() { echo "this is function testA \n"; } } class B { public function testB() { echo "this is function testB \n"; } } class C extends A { publi
class A {
public function testA() {
echo "this is function testA \n";
}
}
class B {
public function testB() {
echo "this is function testB \n";
}
}
class C extends A {
public function __call($method, $args){
$this->b =new B();
try {
return !method_exists ($this->b , $method ) || !$this->b->$method($args[0]);
} catch(Exception $e) {
echo "error";
}
}
}
$object = new C();
$object->testA();
$object->testB();
$object->testD();
如何优化这段代码?PHP中的多重继承由Traits处理,从5.4.0开始提供 更多信息请点击此处:
你说的优化是什么意思?您的实际问题是什么?您不能同时从A和B扩展类。PHP不支持多重继承我知道PHP不支持多重继承。t、 heintz我的意思是如何更改代码,使其运行更快,使用更少的资源。他需要以最佳方式模拟多重继承是的,我只需要更改c类。谢谢,但是我不能更改类A和类B。我只能修改类CSo,将类A和类B的实例作为C构造函数的参数传递,并通过调用magic方法调用这些实例上的方法。
trait A {
public function testA() {
echo "this is function testA \n";
}
}
trait B {
public function testB() {
echo "this is function testB \n";
}
}
class C {
use A, B;
public function __call($method, $args){
// Called method does not exists.
}
}