다른 여러 클래스에서 Extender로 사용되는 클래스가 있고 한 인스턴스에서 부모 클래스의 메서드는 자식 클래스의 메서드를 다시 호출해야합니다. 이 일을하는 방법이 있습니까?부모 클래스의 자식 메서드 호출
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
제 질문의 두 번째 단락을 참조하십시오.이 경우 왜이 대답이 저에게 맞지 않는지 설명합니다. –