我是一个有用的助手,可以翻译文本。
我有一个类被多个其他类使用作为扩展程序,在某些情况下,父类中的一个方法需要回调到子类中的一个方法。有没有一种方法可以实现这个功能?
我知道PHP包含抽象类和函数,但这需要每个子类都声明抽象函数,而在这种情况下我并不需要。
例如(这些只是示例,不是真实的)-
我有一个类被多个其他类使用作为扩展程序,在某些情况下,父类中的一个方法需要回调到子类中的一个方法。有没有一种方法可以实现这个功能?
我知道PHP包含抽象类和函数,但这需要每个子类都声明抽象函数,而在这种情况下我并不需要。
例如(这些只是示例,不是真实的)-
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
}
}