How to declare abstract method in non-abstract class in PHP?

后端 未结 7 503
不知归路
不知归路 2020-12-14 14:51
class absclass {
    abstract public function fuc();
}

reports:

PHP Fatal error: Class absclass contains 1 abstract metho

7条回答
  •  死守一世寂寞
    2020-12-14 14:58

    I wanted to use an abstract method within a non-abstract class (normal class?) and found that I could wrap the method's contents in an 'if' statement with get_parent_class() like so:

    if (get_parent_class($this) !== false) {
    

    Or, in action (tested in a file on cmd line: php -f "abstract_method_normal_class_test.php"):

    
    
    Output:
    I'm dad
    I'm child
    

    PHP get_parent_class() Documentation

提交回复
热议问题