PHP Interfaces & Abstract Classes. When to use which.

Badih Barakat
9 min readJan 30, 2022
PHP Interfaces and Abstract Classes
Photo by Ashkan Forouzani on Unsplash

PHP offers quite a range of OOP features to impower your logic and problem solving abilities. Two of these features which I find so useful are Interfaces and Abstract Classes.

Actually, when look at, they both sound the same at first. Both have methods signatures with no implementations, and both are used in the definition of other classes. But, wait. That’s not the full picture!

Let’s look at each one of them and it’s features, first. Get to know each one better and then decide which one is good for what.

PHP Interfaces

In the words of php.com, interfaces are defined as:

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented. Interfaces share a namespace with classes and traits, so they may not use the same name.

Now, what does that mean? Let’s look at an interface declaration:

interface MyInterface {
public function doThis($a);
public function doThat($b);
}

The interface declaration provides only the required methods signatures. It doesn’t provide any implementations. The class defined using this interface MUST contain all the methods defined in this…

--

--

Badih Barakat

A Software Developer with 15 years of experience on various platforms.