Abstract class and Interface play important role in object-oriented programming. In this lesson, we will try to learn how to use them in PHP.

What is an Abstract Class?

Abstract classes are classes which cannot be directly initialized. Other things to remember about this type of class are:

1. Abstract classes cannot create an object of abstract class
2. Abstract classes are created for inheritance purpose
3. Abstract classes can only be inherited by the child class

Abstract classes in PHP are similar to other OOP languages. You create an abstract class using the abstract keyword.

// #1-Abstract classes cannot create an object of abstract class.

abstract class man { public function run(){ return 1; } } $x = new man; // this will throw an error;
// #2-Abstract classes are created for inheritance purpose.

abstract class parentClass { public function run() { $msg = "Go Run"; echo $msg; } } class childClass extends parentClass { public function walk(){ $msg = "Go Walk"; echo $msg; } } $x = new childClass; $run = $x->run(); $run = $x->walk();