Php 在扩展类的方法之后调用抽象类中的方法?
假设我有一个名为Php 在扩展类的方法之后调用抽象类中的方法?,php,methods,call,Php,Methods,Call,假设我有一个名为MyClass的类,它扩展了MyAbstractClass 在MyClass中有myMethod。在MyAbstractClass中有myAsbtractClassMethod 当myMethod完成时,有没有一种方法可以调用myAsbtractMethod,而不必将parent::myAsbtractClassMethod放在末尾?不,恐怕没有 如果希望实现“执行后钩子”样式的功能,可以设计类来实现_call()魔术方法。例如: <?php abstract class
MyClass
的类,它扩展了MyAbstractClass
在MyClass
中有myMethod
。在MyAbstractClass
中有myAsbtractClassMethod
当
myMethod
完成时,有没有一种方法可以调用myAsbtractMethod
,而不必将parent::myAsbtractClassMethod
放在末尾?不,恐怕没有
如果希望实现“执行后钩子”样式的功能,可以设计类来实现_call()魔术方法。例如:
<?php
abstract class AbstractTest
{
protected function myMethod()
{
echo 'Bar';
}
}
class Test extends AbstractTest
{
public function __call($methodName, $args)
{
call_user_func_array(array($this, $methodName), $args);
call_user_func_array(array('parent', $methodName), $args);
}
protected function myMethod()
{
echo 'Foo';
}
}
$test = new Test();
// Will output 'FooBar'
$test->myMethod();
不,恐怕没有 如果希望实现“执行后钩子”样式的功能,可以设计类来实现_call()魔术方法。例如:
<?php
abstract class AbstractTest
{
protected function myMethod()
{
echo 'Bar';
}
}
class Test extends AbstractTest
{
public function __call($methodName, $args)
{
call_user_func_array(array($this, $methodName), $args);
call_user_func_array(array('parent', $methodName), $args);
}
protected function myMethod()
{
echo 'Foo';
}
}
$test = new Test();
// Will output 'FooBar'
$test->myMethod();
这是一个非常好的方法:)这是一个非常好的方法:)