One of the most important concepts in object oriented programming is abstract class and interface. In this article, we will learn the difference between interface and abstract class in PHP. Before that, we will learn what is the interface and abstract class And how to use them in PHP.
Table of Contents
What is an interface?
Interfaces are declared using the interface keyword. Each method of the interface is an abstract method and must be declared as public.
An abstract method is a method which has a declaration but no implementation i.e. no code is contained in that function.
Basically the interface describes a template and the classes that implement the interface must follow this template i.e. implement the abstract methods of the interface.
Thus, through the use of interfaces, the classes that implement this interface can maintain their strict class structure through the use of specific methods.
Example of an interface
In the following example I have created a CRUDTemplate interface class through which other classes implementing this interface will implement CRUDTemplate’s abstract methods.
<?php
interface CRUDTemplate {
public function add();
public function view();
public function edit();
public function delete();
}
class Category implements CRUDTemplate {
public function add() {
return "Category added";
}
public function view() {
return "Category listed";
}
public function edit() {
return "Category updated";
}
public function delete() {
return "Category deleted";
}
}
class Tag implements CRUDTemplate {
public function add() {
return "Tag added";
}
public function view() {
return "Tag listed";
}
public function edit() {
return "Tag updated";
}
public function delete() {
return "Tag deleted";
}
}
$category = new Category();
echo $category->add();
echo " | ";
echo $category->view();
echo " | ";
echo $category->edit();
echo " | ";
echo $category->delete();
echo "<br />";
$tag = new Tag();
echo $tag->add();
echo " | ";
echo $tag->view();
echo " | ";
echo $tag->edit();
echo " | ";
echo $tag->delete();
?>
The output of the above code is :
Category added | Category listed | Category updated | Category deleted
Tag added | Tag listed | Tag updated | Tag deleted
The above two classes – Category and Tag, implement the CRUDTemplate interface. Now, if any of the Add, View, Edit, Delete methods are missing in Category or Tag class, then PHP will give fatal error.
Example of extendable interfaces
An interface can extend another interface. We will see that in the example below.
<?php
interface CRUDTemplate {
public function add();
public function view();
public function edit();
public function delete();a
}
interface CRUDTemplateUpload extends CRUDTemplate {
public function upload();
}
class Category implements CRUDTemplateUpload {
public function add() {
return "Category added";
}
public function view() {
return "Category listed";
}
public function edit() {
return "Category updated";
}
public function delete() {
return "Category deleted";
}
public function upload() {
return "Category uploaded";
}
}
$category = new Category();
echo $category->add();
echo " | ";
echo $category->view();
echo " | ";
echo $category->edit();
echo " | ";
echo $category->delete();
echo " | ";
echo $category->upload();
?>
The output of the above code is :
Category added | Category listed | Category updated | Category deleted | Category uploaded
Here Category class implements CRUDTemplateUpload interface and this CRUDTemplateUpload interface extends CRUDTemplate interface. Now if any method of add, view, edit, delete and upload is missing in Category class, PHP will throw fatal error.
Example of multiple interface inheritance
An interface can extend multiple interfaces. We will see that in the example below.
<?php
interface CRUDTemplate {
public function add();
public function view();
public function edit();
public function delete();
}
interface CRUDTemplateUpload {
public function upload();
}
interface CRUDTemplateAssign extends CRUDTemplate, CRUDTemplateUpload
{
public function assign();
}
class Category implements CRUDTemplateAssign {
public function add() {
return "Category added";
}
public function view() {
return "Category listed";
}
public function edit() {
return "Category updated";
}
public function delete() {
return "Category deleted";
}
public function upload() {
return "Category uploaded";
}
public function assign() {
return "Category assigned";
}
}
$category = new Category();
echo $category->add();
echo " | ";
echo $category->view();
echo " | ";
echo $category->edit();
echo " | ";
echo $category->delete();
echo " | ";
echo $category->upload();
echo " | ";
echo $category->assign();
?>
The output of the above code is:
Category added | Category listed | Category updated | Category deleted | Category uploaded | Category assigned
Here Category class implements CRUDTemplateAssign interface and this CRUDTemplateAssign interface extends CRUDTemplate and CRUDTemplateUpload interfaces. Now if any method of add, view, edit, delete, upload, and assign is missing in Category class, PHP will throw fatal error.
You can learn more about interface at: https://www.php.net/manual/en/language.oop5.interfaces.php
What is an abstract class?
Abstract classes are declared using the abstract keyword. Abstract class cannot be instantiated but abstract class can be inherited.
An abstract class may or may not have abstract methods but if any class has any abstract method then that class must be abstract class.
An abstract class can describe a template like an interface if the abstract class has an abstract method. In that case, the class that inherits this abstract class must adhere to the class and strict structure, i.e. implement abstract methods.
Example of an abstract class without abstract method
An abstract class is not required to have an abstract method. The code for this example is given below.
<?php
abstract class CRUDTemplate
{
public function add() {
return $this->item(). ' '. 'added';
}
}
class Category extends CRUDTemplate
{
public function item() {
return 'Category';
}
}
$obj = new Category();
echo $obj->add();
?>
The output of the above code is: Category added
In the above example, Category class inherits CRUDTemplate abstract class and Category class is instantiated by using $obj = new Category() but in this way CRUDTemplate abstract class cannot be instantiated. In this example, since the Category class inherits the CRUDTemplate abstract class, the object of the Category class can use the add method of the parent class (CRUDTemplate abstract class).
Example of an abstract class with abstract method
If an abstract class has an abstract method and if another class inherits that abstract class, then that class must implement the abstract method of the abstract class. The code for this example is given below.
<?php
abstract class CRUDTemplate
{
public function add() {
return $this->item(). ' '. 'added';
}
// abstract public function add();
abstract public function view();
abstract public function edit();
abstract public function delete();
}
class Category extends CRUDTemplate
{
public function item() {
return 'Category';
}
/*public function add() {
return "Category listed";
}*/
public function view() {
return "Category listed";
}
public function edit() {
return "Category updated";
}
public function delete() {
return "Category deleted";
}
}
$category = new Category();
echo $category->add();
echo " | ";
echo $category->view();
echo " | ";
echo $category->edit();
echo " | ";
echo $category->delete();
?>
The output of the above code is:
Category added | Category listed | Category updated | Category deleted
In the above example we have used Normal method along with Abstract method.
In the above example, Category class inherits CRUDTemplate abstract class and Category class is instantiated by using $obj = new Category(). In this example, if any method of add, view, edit, and delete, is missing in Category class, PHP will throw fatal error.
Example of abstract class and interface together
In the following example we will see the use of interface and abstract class together. An abstract class that implements an interface may or may not implement any of interface’s abstract methods. But the class that inherits this abstract class must implement the abstract methods of interface and abstract class.
<?php
interface CRUDInterface {
public function add();
public function view();
}
abstract class CRUDAbstract implements CRUDInterface
{
abstract public function edit();
abstract public function delete();
}
class Category extends CRUDAbstract
{
public function add() {
return "Category added";
}
public function view() {
return "Category listed";
}
public function edit() {
return "Category updated";
}
public function delete() {
return "Category deleted";
}
}
$category = new Category();
echo $category->add();
echo " | ";
echo $category->view();
echo " | ";
echo $category->edit();
echo " | ";
echo $category->delete();
?>
The output of the above example is:
Category added | Category listed | Category updated | Category deleted
In the above example, CRUDInterface interface has two abstract methods – add and view and CRUDAbstract abstract class has two abstract methods – edit and delete and Category class inherits CRUDAbstract abstract class and Category. CRUDAbstract class implements CRUDInterface interface but this abstract class does not implement any abstract methods of the interface.
The classes that extend the abstract class must implement the abstract methods. So, in this case if any method of add, view, edit, and delete is missing in Category class, PHP will throw fatal error.
You can learn more about abstract class at: https://www.php.net/manual/en/language.oop5.abstract.php
Difference between interface and abstract class in PHP
In this article we discussed interface and abstract class with definition and multiple examples. Now we will sum up the differences between these two.
# | Interface | Abstract class |
1 | Interface can have only abstract methods | Abstract class can have abstract methods and non abstract methods |
2 | Interface can have only public methods | Abstract class can have public or protected methods |
3 | The abstract keyword must not be given before the abstract method of the interface | The abstract keyword must be given before the abstract method of the abstract class |
4 | Interface can not have properties | Abstract class can have properties |
5 | Multiple Interface Implementation is possible – means a class can implement multiple interfaces. | Abstract class does not support multiple inheritances – means a class can only inherits (extends) one parent class whether it is abstract or concrete. |