16

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 PHP offers pure virtual functions. I want the following

class Parent {
   // no implementation given
   public function foo() {
      // nothing 
   }
}

class Child extends Parent {
   public function foo() {
      // implementation of foo goes here
   }
}

Thanks very much.

ale
  • 11,636
  • 27
  • 92
  • 149

4 Answers4

21

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
   }
}
daiscog
  • 11,441
  • 6
  • 50
  • 62
  • 1
    A pure virtual function requires that is defined in a sub-class. If not an error should occur. Will an error occur if foo() is not defined. This is key. –  Jul 14 '12 at 19:34
  • Yes, if you extend `Parent` but do not define `foo` you will get an error (unless your subclass is also abstract, in which case it cannot be instantiated and any non-abstract subclasses must define `foo` themselves). For more information, see [the PHP manual](http://php.net/manual/en/language.oop5.abstract.php), which states that "all methods marked abstract in the parent's class declaration must be defined by the child". – daiscog Jul 16 '12 at 16:38
  • And when a class is declared `abstract` does it mean all it's functions must be `abstract` (that is, `pure virtual`), or you may create virtual and final functions (I mean functions not prepended with the `abstract` keyword)? – Silidrone Jan 14 '21 at 17:47
  • @Silidrone An abstract class may have abstract or concrete functions. See [the docs](https://www.php.net/manual/en/language.oop5.abstract.php) for more details. – daiscog Jan 15 '21 at 08:43
4

Declare the method as abstract in the Parent class:

abstract public function foo();
stivlo
  • 83,644
  • 31
  • 142
  • 199
3

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
   }
}
Davide
  • 2,329
  • 1
  • 14
  • 12
1

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

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Creating an empty method without declaring the method as abstract (like in the question) does not reflect C++ pure-virtual method behaviour (which is the same as abstract methods in PHP). – daiscog Sep 29 '11 at 17:07
  • 1
    This is the best answer in the world for anything ever. I hope that one day when my kids grow up I can show them this answer and they can be impressed as much as I am right now. Generations of programmers are better because of it, you probably single handedly saved millions of lives here - well done sir, well done. Have a +1. – Benjamin Gruenbaum Feb 13 '15 at 18:15