给定扩展类B的类A,我如何让对类A的__call函数的调用覆盖从父类继承的匹配函数?
考虑这个简化的例子:
class A
{
public function method_one()
{
echo "Method one!\n";
}
}
class B extends A
{
public function __call($name, $args)
{
echo "You called $name!\n";
}
}
$b = new B();
$b->method_one();当我运行它时,我得到了输出的Method one!。我想要得到输出的You called method_one!。
那么,如何让子类的魔术方法覆盖父类定义的方法呢?
我需要扩展该对象,因为我需要访问A中的一个受保护的方法,但我希望将所有公共方法导入到我自己的__call处理程序中。有没有办法做到这一点?
发布于 2013-05-01 18:37:10
尝尝这个
class A1
{
protected function method_one()
{
echo "Method one!\n";
}
}
class B1
{
private $A;
public function __construct()
{
$this->A = new A1;
}
public function __call($name, $args)
{
$class = new ReflectionClass($this->A);
$method = $class->getMethod($name);
$method->setAccessible(true);
//return $method;
echo "You called $name!\n";
$Output=$method->invokeArgs($this->A, $args);
$method->setAccessible(false);
return $Output;
}
}
$a = new B1;
$a->method_one("");https://stackoverflow.com/questions/16316385
复制相似问题