Shows the list having the differences between interfaces and abstract classes in PHP.

Interface Vs Abstract Class In PHP
January 20, 2020
Interface Vs Abstract Class In PHP

Interface Vs Abstract Class

PHP is among the most popular web development languages and widely used for website and web application development. Since the release of PHP 5 in 2004, OOP was introduced in PHP and it continuously evolved to the present where it's very easy to develop software in PHP by applying OOPs concepts. The most recent versions of PHP since PHP 7.0 have focused a lot on supporting types including return type declaration. Interfaces and Classes are the most important aspect of any OOP language and considered as the base for designing good software that can be easily understood, extended and managed by others. This tutorial explains the differences between an Interface and Abstract Class with examples.

We can use both the Interfaces and Abstract classes for abstraction to leave the actual implementation by just designing the prototype. The actual implementation can be done by the classes implementing the interfaces and the abstract classes. An Interface or Abstract Class cannot be considered as fully implemented until all the abstract functions are defined.

This post provides a list of differences between the Interface and Abstract Class in PHP. You can also refer Interface Vs Abstract Class In PHP for example.

Interface Vs Abstract Class In PHP

Interface Abstract Class
An interface cannot use abstract modifier. An Abstract Class must use abstract modifier.
An interface cannot use public, protected, and private access modifiers. An Abstract Class cannot use public, protected, and private access modifiers.
An interface can be declared using interface modifier. Abstract Class can be declared using abstract and class modifiers.
Interfaces cannot use final modifier. Abstract Class cannot use final modifier.
Interfaces can declare constants using the const modifier. Abstract Classes can declare constants using the const modifier.
Interfaces cannot declare public, protected, private, static and final variables. Abstract Classes can declare public, protected, private, static and final variables.
All the functions declared in an Interface are inherently public and abstract. Functions in an Abstract Class must be exclusively declared as public and abstract where required.
The function definition is not allowed in an Interface. We can alternatively use Traits to declare and define functions and to reuse the functions. Functions can be defined in an Abstract Class. Abstract Classes can use Traits.
Interfaces cannot declare private, protected, and final methods. Abstract Class can declare and define private, protected, and final methods.
An Interface can extend either single or multiple Interfaces using the keyword extends. An Abstract Class can extend only a single Abstract Class or Concrete Class using the keyword extends.
An Interface cannot implement another Interface or extend Abstract Class. An Abstract Class can implement either single or multiple Interfaces using the keyword implements.
Interfaces are always 100% abstract. An Abstract Class can be 100% abstract if all the methods declared within the class are abstract.
Interfaces can be used when several classes are required to implement the same methods. Abstract Class can be used to partially implement the features of child classes.
Interfaces can be used for multiple inheritance. Abstract Class can extend another Abstract Class or Concrete Class and implement multiple Interfaces for multiple inheritance.

Interface Example

interface A {

const DEFAULT_NAME = "Guest";

public abstract function printName(string $name);
}

Abstract Class Example

abstract class CA {

const DEFAULT_NAME = "Guest";

public abstract function printName(string $name);

public function printNumber(int $number) {

echo $number;
}
}
Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS