What is the Difference Between Abstract Class and Interface in PHP

The main difference between Abstract Class and Interface in PHP is that abstract class can consist of abstract methods and non-abstract methods while all the methods in an interface have no implementations.

PHP is a server-side scripting language. It is one of the most popular languages in web development. PHP allows handling files, handling forms, sending emails, uploading files, connecting an application with DBMS such as MySQL and many other tasks. Frameworks such as Zend and Code Igniter are based on PHP.  PHP also supports Object Oriented Programming (OOP).  One major concept in OOP is abstraction. It helps to hide implementations and to display only the functionality to the user. Abstract class and interface are two methods to achieve abstraction.

Key Areas Covered

1.  What is Abstract Class in PHP
      – Definition, Functionality
2. What is Interface in PHP
      – Definition, Functionality
3. Difference Between Abstract Class and Interface in PHP
      – Comparison of Key Differences

Key Terms

Abstraction, Abstract Class, Interface, OOP, PHP

Difference Between Abstract Class and Interface in PHP - Comparison Summary

What is Abstract Class in PHP

An abstract method is a method that does not have any implementation. An abstract class is a class that has at least one abstract method. It can have abstract methods as well as non-abstract method. The keyword “abstract” is used to declare an abstract class. Moreover, the class that extends the abstract class should provide the implementations for the abstract methods in that class. Programmers cannot create objects using an abstract class, but they can create an object from a class that extends the abstract class.

Difference Between Abstract Class and Interface in PHP

Figure 1: Abstract class in PHP

In the above image, Shape is an abstract class. It has an abstract method called draw and a non-abstract method called display. The triangle class extends Shape. So, the triangle class provides the implementation for the abstract method draw. In line 19, an object of Triangle is created. In line 20, the draw method is called using that object. Therefore, the draw method executes and it displays the “Triangle” on the console.

What is Interface in PHP

All the methods in an interface are abstract methods. In other words, it only contains function prototypes or functions without implementations. Programmer cannot create objects using an interface. An interface is declared using the “interface” keyword. Even though the methods in an interface are abstract, the programmer does not declare them with the abstract keyword. Instead, those methods must have the public visibility scope. A class that implements an interface should provide the implementations for all its abstract methods.

Main Difference - Abstract Class vs Interface in PHP

Figure 2: PHP program 1 with an interface

In the above program, Shape is an interface. It has a method called draw. It does not have an implementation. The class Triangle extends Shape. The Triangle class provides the implementation for the draw method. In line 16, an object of Triangle object is created. In line 17, the draw method is called using that object. Therefore, the draw method executes and displays the “Triangle” on the console.

Furthermore, interfaces also help to implement multiple inheritance. Multiple inheritance is the process of allowing a class to use attributes and methods of multiple classes. 

Abstract Class vs Interface in PHP_Figure 3

Figure 3: PHP program with interfaces

Interface A has a method methodA. Similarly, interface B has a method methodB. These methods are function prototypes. In other words, these methods do not have any implementations. Class Test implements interface A and B. Therefore, that class provides implementations for methodA and methodB. Class Test also has its own method called methodTest. Line 26, creates an object of Test. Then the methodA, method, and methodTest are called using that object.

Difference Between Abstract Class and Interface in PHP       

Definition

An abstract class in PHP is a class declared with an abstract keyword and is a collection of abstract and non-abstract methods. In contrast, an interface in PHP is a reference type and consists of a collection of methods with no implementations or function prototypes. Thus, this is the main difference between Abstract Class and Interface in PHP.

Methods

An abstract class can have abstract methods as well as non-abstract methods while all the methods in an interface are method without implementations.

Keyword

While an abstract class is declared with the “abstract” keyword, an interface is declared with the “interface” keyword.

Usage

Another difference between Abstract Class and Interface in PHP is that an abstract class helps to achieve abstraction while an interface helps to achieve abstraction as well as multiple inheritance.

Conclusion

PHP language supports Object Oriented Programming. Abstraction is a pillar of OOP. Abstract classes and interfaces are two methods of achieving abstraction. The main difference between Abstract Class and Interface in PHP is that the abstract class can consist of abstract methods and non-abstract methods while all the methods in an interface have no implementations.

References:

1.“Abstract Classes in PHP.” GeeksforGeeks, 11 Dec. 2018, Available here.
2.“Multiple Inheritance in PHP.” GeeksforGeeks, 21 Feb. 2019, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply