Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 391
ale Avatar asked Sep 29 '11 17:09

ale


3 Answers

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
   }
}
like image 141
daiscog Avatar answered Oct 21 '22 02:10

daiscog


Declare the method as abstract in the Parent class:

abstract public function foo();
like image 31
stivlo Avatar answered Oct 21 '22 01:10

stivlo


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
   }
}
like image 43
Davide Avatar answered Oct 21 '22 01:10

Davide