Are there pure virtual functions in PHP like with C++

前端 未结 4 676
情话喂你
情话喂你 2021-02-13 06:17

I would have thought lots of people would have wondered whether this is possible but I can\'t find any duplicate questions... do correct me.

I just want to know whether

相关标签:
4条回答
  • 2021-02-13 06:58

    You can create abstract functions, but you need to declare the parent class as abstract, too:

    abstract class Parent {
       // no implementation given
       abstract public function foo();
    }
    
    class Child extends Parent {
       public function foo() {
          // implementation of foo goes here
       }
    }
    
    0 讨论(0)
  • 2021-02-13 07:05

    There are abstract classes!

    abstract class Parent {
       // no implementation given
       abstract public function foo();
       }
    }
    
    class Child extends Parent {
       public function foo() {
          // implementation of foo goes here
       }
    }
    
    0 讨论(0)
  • 2021-02-13 07:07

    Declare the method as abstract in the Parent class:

    abstract public function foo();
    
    0 讨论(0)
  • 2021-02-13 07:08

    Yes, that type of solution is possible, it's called polymorphism, you can do it without declaring an abstract class or an interface.

    0 讨论(0)
提交回复
热议问题