Abstract classes and interfaces are two advanced PHP OOP constructs. They both work as blueprints for other classes.
In the case of abstract classes, other classes can use inheritance to become specific types of the parent, abstract class. In the case of interfaces, classes can implement them to acquire specific functionalities.
But what are the differences between abstract classes and interfaces? When do you need to use one, and when the other?
Let's find out in today's email.
P.S. You can learn more about abstract classes and interfaces here:
An abstract class is a class that is not complete. You cannot create an object of that class. Abstract classes can contain abstract methods: methods that only define their name and argument list (the so-called signature), but that lack the body.
You can define children classes that inherit from an abstract class. These children classes share the same type with the abstract class. Children classes must implement the abstract methods from the parent class.
Interfaces in a nutshell.
Interfaces are similar to abstract classes, but they are not classes at all. They provide a list of method signatures (name and argument list, without the body), which are much like abstract methods. The purpose of interfaces is to provide a common set
of functionalities for other classes.
Classes do not inherit from interfaces. Instead, they implement them. When they do so, they must implement all the interface's methods and make them publicly available in the class.
The differences between Abstract Classes and Interfaces.
First of all, let's look at what abstract classes and interfaces have in common:
They both work as blueprints for other classes.
You cannot create objects of abstract classes or interfaces.
They both provide abstract methods that other classes must implement.
They support inheritance: abstract classes can inherit from other abstract classes, and interfaces can inherit from other interfaces.
Now let's take a look at the most important differences:
Abstract classes support methods, constants and properties. Interfaces only support
methods and constants.
In abstract classes, you can define abstract methods and non-abstract methods as well. Interfaces only provide abstract methods.
Abstract classes support public, protected and private methods, constants and properties. Interfaces only support public methods and constants.
PHP does not support multiple inheritance: a class can only inherit from one class. However, a class can implement multiple interfaces at the same time.
Usage differences: when to use one and when the other.
And now we've arrived at the most important question: when to use which?
In a nutshell, you want to use class abstraction and inheritance when you want more classes to share the same type. If there are some attributes (properties and methods) that are common to a particular
type, then class abstraction is the way to go.
For example, you can define an abstract class named Bird, and then define children classes such as Owl, Parrot and Pigeon. You can find more details in this tutorial.
However, sometimes class abstraction does not really work well. For instance, there are cases when completely different classes need to share the same set of functions, without sharing the same type. In these cases, you want to use interfaces.
For example, let's say that you want some classes to be able to print their data for debug purposes. You can define a Debuggable interface, which provides
a printDebug() method. All classes that implement Debuggable will implement that method, no matter their type. This way, all those classes will provide the same printDebug() functionality, but they will be free to inherit from different parent classes.
How do you write PHP code that is always secure from attacks? In this course you will learn the defense techniques that really work, leaving nothing to chance.