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

后端 未结 7 508
不知归路
不知归路 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 15:09

    See the chapter on Class Abstraction in the PHP manual:

    PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation.

    It means you either have to

    abstract class absclass { // mark the entire class as abstract
        abstract public function fuc();
    }
    

    or

    class absclass {
        public function fuc() { // implement the method body
            // which means it won't be abstract anymore
        };
    }
    

提交回复
热议问题