我有被其他几类用作扩展一个类,并在一个事例,从父类的方法需要回调从子类中的方法。有没有办法做到这一点?调用子方法与父
我意识到PHP包含abstract
类和函数,但要求每个子类具有声明的abstract
函数,在这种情况下我不需要这些函数。
例如(这些例子,并不是真实的生活) -
Class parent{
function on_save_changes(){
some_parent_function();
if($_POST['condition'] === 'A') :
// Call 'child_1_action()'
elseif($_POST['condition'] === 'B') :
// Call 'child_2_action()'
endif
some_other_parent_function();
}
function some_parent_function(){
// Do something here, required by multiple child Classes
}
}
Class child_1 Extends parent{
function __construct(){
$this->on_save_changes();
}
function child_1_action(){
// Do something here, only every required by this child Class
}
}
Class child_2 Extends parent{
function __construct(){
$this->on_save_changes();
}
function child_2_action(){
// Do something here, only every required by this child Class
}
}
还有另一个线程在stackoverflow与相同的查询: http://stackoverflow.com/questions/1944827/php-how-to-call-function-of-a-child-class-from-parent-class – maxxon15
请参阅我的问题的第二段,解释为什么在这种情况下,该答案不适合我。 –