What is an abstract class in PHP and when to use an abstract class in your application? In this tutorial, we’ll learn about abstract class and their implementation.
Abstract Class in PHP
An abstract class is a class that contains at least one abstract method. An abstract method is simply a function definition without any actual code in it. An abstract method contains a method name and the parameters of a method with an abstract keyword at the beginning of a method.
An Abstract class acts as a blueprint for a class which inherits them. A child class which inherits an abstract class needs to implement their abstract methods. An abstract class is only created for an inheritance, it means you can’t create their object directly.
Important Points about Abstract Class
1. An Abstract Class cannot be instantiated, It means you cannot use them directly.
2. An abstract method should not be private.
3. When any child class inherits an abstract class then all the abstract methods in a parent class must be defined in a child class.
4. An Abstract class can also have non-abstract (concrete) methods.
5. A class cannot extend multiple abstract classes but they can implement multiple interfaces.
When to use an Abstract Class in PHP
The real question is when to use an abstract class in an application.
Use an abstract class, when you need to implement a common functionality across multiple classes. In this way, your code is organized and well structured. The purpose of an abstract class is to provide some functionality and leave rest to the derived class.
** Apart from using abstract classes, You can also use PHP Traits and interface to make code reusable and well structured.
Example of Abstract Class
To understand this concept, let’s create an abstract class car. Every car has their own model type such as BMW, Swift, HondaCity etc. which they need to define. And each car shares some common functionality such as all car have four wheels.
Let’s create a class car. In a car class, I’ll create an abstract method modelType.
Every car has four wheels, so let’s define a concrete method wheelcount(). Every child class which inherits car class must define their own model type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
abstract class car{ /* Abstract method, this must be define in child class. */ abstract public function modelType(); /* Concrete method, which is same in all classes. */ public function wheelCount() { echo "I have four wheels"; } } |
You can’t create an object of an abstract class directly. Let’s check what happens if we try to create an object of an abstract class.
$data = new car;
PHP Fatal error: Cannot instantiate an abstract class.
Let’s create a child class, which inherits an abstract class.
1 2 3 4 5 6 7 8 9 10 11 |
class HondaCity extends car{ public function modelType(){ echo "HondaCity"; } } $car = new HondaCity; $car->modelType(); /* Common method, which can be accessed using a child class object. */ $car->wheelCount(); |
1 2 3 4 5 6 7 8 9 10 |
class Swift extends car { public function modelType() { echo "swift"; } } $car = new Swift; $car->modelType(); $car->wheelCount(); |
Example 2 – Let’s take an another example to understand an abstract class.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//An abstract class shape abstract class Shape { abstract protected function getColor(); abstract protected function setColor($color); //Common method available for all the classes which extends shape class public function describe() { return sprintf("I'm an %s %s\n", $this->getColor(), get_class($this)); } } |
Let’s create a circle class which extends an abstract class shape
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Class circle extends shape class class Circle extends Shape { private $color = null; public function getColor() { return $this->color; } public function setColor($color) { $this->color = $color; } } //Instantiate the Circle class $circle = new Circle(); //Set the color $circle->setColor('Orange'); // Print the description echo $circle->describe(); |
Similarly, we can create another class square which can extend our shape class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Class square extends shape class class Square extends Shape { private $color = null; public function getColor() { return $this->color; } public function setColor($color) { $this->color = $color; } } //Instantiate the Square class $square = new Square(); //Set the color $square->setColor('Orange'); // Print the description echo $square->describe(); |
For more information, you can check php.net documentation.
Conclusion
By using an abstract class in your code, you can provide a blueprint for a child classes.