php abstract class extending another abstract class

前端 未结 3 2127
借酒劲吻你
借酒劲吻你 2021-01-03 17:42

Is it possible in PHP, that an abstract class inherits from an abstract class?

For example,

abstract class Generic {
    abstract public function a(         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-03 18:20

    It will work, even if you leave the abstract function b(); in class MoreConcrete.

    But in this specific example I would transform class "Generic" into an Interface, as it has no more implementation beside the method definitions.

    interface Generic {
        public function a(); 
        public function b();
    }
    
    abstract class MoreConcrete implements Generic {
        public function a() { do_stuff(); }
        // can be left out, as the class is defined abstract
        // abstract public function b();
    }
    
    class VeryConcrete extends MoreConcrete {
        // this class has to implement the method b() as it is not abstract.
        public function b() { do_stuff(); }
    }
    

提交回复
热议问题