Updated March 17, 2023
What is an Abstract Class?
Like abstract class, there are abstract methods as well. We declare both the abstract method and the abstract class with the abstract keyword. In this topic, we are going to learn about the Abstract class in PHP.
As per the concept of inheritance, the parent class is extended by the derived class. The methods in the parent class are implemented or defined by the derived classes. PHP makes way for the parent class to be more specific by making the use of abstract class and abstract methods.
The abstract class compulsorily contains one method as abstract. Also, this abstract class can have other non-abstract methods as well.
Syntax
Syntax of Abstract Class with one method as abstract.
abstract class DemoAbstractClass() {
abstract public function DemoAbstractMethod();
}
Abstract Method
abstract public function DemoAbstractMethod();
How does Abstract Class work in PHP?
This article will learn the working of the abstract class, and it goes like this.
Now, as we know that an abstract class compulsorily has one method as abstract.
There can be non-abstract methods also. The abstract method has only the declaration in the base class. Meaning that it has only names and parameters with no other code.
To define the method further and to work with the method, this method needs to be implemented in the derived class, which extends the base class. Also, remember that this abstract class cannot be instantiated to create objects, but the class derived from the base class can be instantiated to create objects.
Examples of Abstract Class in PHP
Here are some examples of Abstract class in PHP given below
Example #1
In the below program, we learn what happens when an object of an abstract class is created.
Abstract class Student is created containing one abstract method favouriteSubject() method of the Student Class and two other not abstract methods like setRollNo() and getRollNO() which sets and gets the student’s role, which is done by creating the object of the derived class (extending the base class).
Code:
<?php
abstract class Student {
protected $m;
public function setRollNo($rollno) {
return $this->m = $rollno;
}
public function getRollNo() {
return $this->m;
}
abstract public function favouriteSubject()
}
class Radha extends Student {
public function favouriteSubject() {
return "English";
}
}
$obj = new Student; //this statement throws error as we cannot create object of class Student as it is defined as abstract
$obj = new Radha; //this statement does not throws error and executes correctly
$obj->favouriteSubject();
?>
Output :
Example #2
This example is explained step by step below, after the program output along with the code from this example.
Code
abstract class Student{
protected $m;
abstract public function calulatePercentage();
public function setMarks($marks) {
return $this->m = $marks;
}
public function favoriteSubject() {
return 'Favorite subject is English';
}
}
class Ram extends Student{
public function calulatePercentage(){
$percentage = ($this->m /100 ) * 100;
return 'The percentage of Ram is '.$percentage. '%' .'<br>';
}
}
class Sherry extends Student{
public function calulatePercentage(){
$percentage = ($this->m /100 ) * 100;
return 'The percentage of Sherry is '.$percentage. '%' .'<br>';
}
public function favoriteSubject() {
return 'Favorite subject is Maths';
}
}
$ram = new Ram();
$ram->setMarks(92);
echo $ram->calulatePercentage();
echo $ram->favoriteSubject();
echo '<hr>';
$sherry = new Sherry();
$sherry->setMarks(97);
echo $sherry->calulatePercentage();
echo $sherry->favoriteSubject();
Output :
An abstract class can be termed as a skeleton for derived classes. In the above example, we declare the abstract class Student and a property called $m for marks of the student.
Explanation of Example #2
abstract class Student {
abstract public function calulatePercentage();
}
An abstract class can be termed as a skeleton for derived classes. In this example, we declare the abstract class and methods along with other methods.
In the above example, we have declared abstract class Students with abstract methods to calculate the percentage of the student, along with marks given.
Also, we have created methods (which are not abstract) to set the Marks of the student and to get the favorite subject of the student.
abstract class Student{
abstract public function calulatePercentage();
public function setMarks($marks) {
return $this->m = $marks;
}
public function favoriteSubject() {
return 'Favorite subject is English';
}
}
To create objects of abstract class Student, we have to extend the class Student and create derived classes out of it. The base class uses extends keywords to allow the base class to extend. Once the class is extended, we can now use the base class methods.
In the above example, Ram is the derived class that extends the base class Student. It uses the extends keyword. Now we have calculated the percentage() method to calculate the percentage of the marks obtained by Ram.
class Ram extends Student{
public function calulatePercentage(){
$percentage = ($this->m /100 ) * 100;
return 'The percentage of Ram is '.$percentage. '%' .'<br>';
}
}
Declaring one more class which extends the base class Student for us to learn more.
In the above example, we have created Sherry as the derived class created from the base class Student. This uses the extended keyword. It has two functions: abstract, the calulatePercentage() method in the base class, the other is the favoriteSubject() method, which is not abstract.
class Sherry extends Student{
public function calulatePercentage(){
$percentage = ($this->m /100 ) * 100;
return 'The percentage of Sherry is '.$percentage. '%' .'<br>';
}
public function favoriteSubject() {
return 'Favorite subject is Maths';
}
}
Now we create the object of the base class and the object of the derived class, which throws an error. Because we cannot instantiate, the class is declared as abstract.
Thus only the object of the derived class can be created. Once this is done, using this object, we will call the abstract method and not the abstract method, both as seen below.
$ram = new Ram();
$ram->setMarks(92);
echo $ram->calculatePercentage();
echo $ram->favoriteSubject();
echo '<hr>';
$sherry = new Sherry();
$sherry->setMarks(97);
echo $sherry->calculatePercentage();
echo $sherry->favoriteSubject();
Conclusion
In this article, I hope you learned about how the abstract class is declared, how it works, how it is extended using extends keywords, how the abstract method is declared in the base class and how it is implemented in the derived class. The examples explained will help you learn the concept at ease. I hope it was prepared to be useful enough to grasp and practice more.
Recommended Articles
This is a guide to the Abstract class in PHP. Here we discuss the introduction, working and examples of Abstract Class in PHP with the sample code and outputs. You may also have a look at the following articles to learn more –