Use an interface when you want to force developers working in your system (yourself included) to implement a set number of methods on the classes they'll be building.

Use an abstract class when you want to force developers working in your system (yourself included) to implement a set numbers of methods and you want to provide some base methods that will help them develop their child classes.

Another thing to keep in mind is client classes can only extend one abstract class, whereas they can implement multiple interfaces. So, if you're defining your behavior contracts in abstract classes, that means each child class may only conform to a single contract. Sometimes this a good thing, when you want to force your user-programmers along a particular path. Other times it would be bad. Imagine if PHP's Countable and Iterator interfaces were abstract classes instead of interfaces.

One approach that's common when you're uncertain which way to go (as mentioned by cletus below) is to create an interface, and then have your abstract class implement that interface.

Answer from Alana Storm on Stack Overflow
Top answer
1 of 11
527

Use an interface when you want to force developers working in your system (yourself included) to implement a set number of methods on the classes they'll be building.

Use an abstract class when you want to force developers working in your system (yourself included) to implement a set numbers of methods and you want to provide some base methods that will help them develop their child classes.

Another thing to keep in mind is client classes can only extend one abstract class, whereas they can implement multiple interfaces. So, if you're defining your behavior contracts in abstract classes, that means each child class may only conform to a single contract. Sometimes this a good thing, when you want to force your user-programmers along a particular path. Other times it would be bad. Imagine if PHP's Countable and Iterator interfaces were abstract classes instead of interfaces.

One approach that's common when you're uncertain which way to go (as mentioned by cletus below) is to create an interface, and then have your abstract class implement that interface.

2 of 11
200

The differences between an Abstract Class and an Interface:

Abstract Classes

An abstract class can provide some functionality and leave the rest for derived class.

  • The derived class may or may not override the concrete functions defined in the base class.

  • A child class extended from an abstract class should logically be related.

Interface

An interface cannot contain any functionality. It only contains declaration of the methods.

  • The derived class MUST provide code for all the methods defined in the interface.

  • Completely different and non-related classes can be logically grouped together using an interface.

๐ŸŒ
Medium
medium.com โ€บ @Amir_M4A โ€บ in-the-world-of-php-programming-there-are-several-key-concepts-that-developers-need-to-understand-b2ed1916287f
PHP: Abstract Classes, Interfaces, and Traits Explained | Medium
September 23, 2024 - The Car and Motorcycle classes implement the Vehicle interface and provide their own implementation of these methods. A trait in PHP is a mechanism for code reuse that enables developers to reuse methods and properties in multiple classes. Traits are similar to abstract classes and interfaces, but they allow for horizontal code reuse rather than vertical inheritance.
Discussions

When to use abstract classes vs interfaces and traits?
When is it best to use abstract classes over interfaces and traits? When is it best to use interfaces and traits over abstract classes? This is a false dichotomy. All three can be used in unison: Interfaces are used to provide an interface to communicate with, as well as a blueprint for classes to use to implement methods. Abstract classes are used to provide partial implementation, as well as a base type which facilitate communication and extendibility. Traits are used for code re-use. It can't be used for type hinting. If you're doing dependency injection, you'd want to use interfaces to be able to replace the implementation in the future if you choose to. If you have multiple sub-types, you might want to have a base type to share implementation between these (A good example is having a Request type from which you extend for the various HTTP verbs). Traits then allow you to group functionality which might not only apply to that inheritance tree. More on reddit.com
๐ŸŒ r/PHP
19
29
October 8, 2012
Base Class & Interfaces or Abstract Classes - I'm lost when to use what
Great question. There is so much confusion on the matter. First of all, technically here is no such thing in PHP as a "Base class". And Abstract class can be considered a good enough substitution, so you can think of Abstract class as a Base class. Hence we have only two matters left, Abstract class and Interface. Although they have overlapping responsibility, I devised a simple method to tell them apart: an Interface is sort of a public contract: it faces outwards. It tells other classes, what to expect from our class. Not to mention that a class could implement many Interfaces. an Abstract class is sort of a blueprint for other classes. It faces inwards. It tells other classes of the same type what they should be. And also implements the common functionality. Based on the above, this is how I do primitive class hierarchies I am able to design: decide the public contract my class has to offer, and create an Interface decide the inner workings and common functionality and create an Abstract class proceed to creating actual classes that extend the Base Abstract class (and already implements all its interfaces, though some Interfaces can be added to that particular class as well). So it could be like interface DeviceInterface { public function getDevice(); } abstract class AbstractDevice implements DeviceInterface { protected $device; public function getDevice() { return $this->device; } } class MobileDevice extends AbstractDevice { public function __construct() { $this->device = 'iPhone'; } } $device = new MobileDevice(); echo $device->getDevice(); More on reddit.com
๐ŸŒ r/PHPhelp
17
8
September 17, 2024
Any reason to use an interface over an abstract class?
Multiple interfaces can be used on a single class, but you can only extend a class once Abstract classes can be more than just function definitions - it's a regular class, you just can't use it without extending it. Abstract classes are good when you have a base object that has children which will need to implement their own versions of some functions. The parent (abstract) can define all of the shared functions, and the children inherit all of those automatically. An interface requires you to code every one of those functions (or inherit them from another class). More on reddit.com
๐ŸŒ r/PHP
12
1
August 31, 2011
When should I use an interface/abstract class?

An interface/abstract class basically guarantees that an inheriting class will define a particular method.

Here's an example which isn't necessarily PHP, but easy to understand. Say you're making a space shooter game, where you have ships, asteroids and bullets.

Though they're different, they share many things in common: They need to move, they need to be rendered, they need to handle collisions.

Rendering is the same for all of these objects: simply draw the object at its current co-ordinates on the screen.

Collisions and movement are different though: a bullet moves in a straight line and disappears upon collision. An asteroid moves slowly, while rotating, and explodes into smaller asteroids when hit. A ship's movement requires input and more complex physics.

You could use an abstract 'GameEntity' class to define a 'render()' method, which does the same for each entity, and abstract methods for 'movement()' and 'collision($with)', which are defined by the implementing classes themselves. Then you can use a simple loop to iterate through all of your GameEntity objects calling render(), movement() and collision(), knowing that the methods exist, without having to know what type of entity you're dealing with.

The advantage of this is that if you want to add a new entity, such as a UFO, you simply inherit the abstract class and you know that your existing code will be compatible without modification specifically to account for the UFO class.

More on reddit.com
๐ŸŒ r/PHP
14
11
February 26, 2012
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_oop_interfaces.asp
PHP OOP Interfaces
All methods in an interface are abstract, so they cannot be implemented in code and the abstract keyword is not necessary ยท Classes can implement an interface while inheriting from another class at the same time ยท To implement an interface, ...
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ difference-between-abstract-classes-vs-interfaces-php-usman-malik-fi9ef
Difference Between Abstract Classes Vs Interfaces in PHP
February 6, 2024 - They contain only method declarations (abstract methods) and constants, but no method implementations. Usage:Interfaces are used to define a contract for classes that may not share a common hierarchy but need to provide certain functionalities.
๐ŸŒ
Reddit
reddit.com โ€บ r/php โ€บ when to use abstract classes vs interfaces and traits?
r/PHP on Reddit: When to use abstract classes vs interfaces and traits?
October 8, 2012 -

I came across this answer for how to use interfaces and traits in unison: http://stackoverflow.com/a/9205347

Could someone explain to me or suggest some reading material as to how abstract classes fit into that answer? Do interfaces and traits used together remove the need for abstract classes?

When is it best to use abstract classes over interfaces and traits? When is it best to use interfaces and traits over abstract classes?

Edit: Thanks to everyone who responded. You have been very welcoming to a new redditor.

Top answer
1 of 5
16
When is it best to use abstract classes over interfaces and traits? When is it best to use interfaces and traits over abstract classes? This is a false dichotomy. All three can be used in unison: Interfaces are used to provide an interface to communicate with, as well as a blueprint for classes to use to implement methods. Abstract classes are used to provide partial implementation, as well as a base type which facilitate communication and extendibility. Traits are used for code re-use. It can't be used for type hinting. If you're doing dependency injection, you'd want to use interfaces to be able to replace the implementation in the future if you choose to. If you have multiple sub-types, you might want to have a base type to share implementation between these (A good example is having a Request type from which you extend for the various HTTP verbs). Traits then allow you to group functionality which might not only apply to that inheritance tree.
2 of 5
2
Kore Nordmann just blogged about this a couple of days ago: http://qafoo.com/blog/026_abstract_classes_vs_interfaces.html His view is a bit different then the previously stated one. He basically says that interfaces tend to lead to god classes (because you can implement multiple interfaces, but only inherit one class). He suggests only using interfaces for general abstractions like Cachable, Countable, Traversable, Filtering, Validating, etc. So if you're solving a very specific problem that has a specific interface, you're better of writing an abstract class with some abstract methods. This will force everyone to only solve this one problem when extending your class interface (the methods, not the language construct).
๐ŸŒ
Andrew Schmelyun
aschmelyun.com โ€บ blog โ€บ the-difference-between-traits-interfaces-and-abstract-classes-in-php
The difference between Traits, Interfaces, and Abstract Classes in PHP - Andrew Schmelyun
August 7, 2022 - A Trait is a group of properties ... organization and reducing repetition. An Interface is a set of method signatures to enforce a particular implementation in the class they're added to....
๐ŸŒ
Scaler
scaler.com โ€บ topics โ€บ php-tutorial โ€บ interface-and-abstract-class-in-php
Abstract Classes and Interfaces in PHP - Scaler Topics
November 9, 2023 - They consist only of method signatures and constants, without any implementation. Classes can implement multiple interfaces but can inherit from only one abstract class. Both abstract classes and interfaces provide a way to enforce structure, standardize behavior, and promote code reusability in PHP applications.
Find elsewhere
๐ŸŒ
Ash Allen Design
ashallendesign.co.uk โ€บ blog โ€บ interfaces-vs-abstract-classes-in-php
Interfaces vs Abstract Classes in PHP | Ash Allen Design
January 2, 2024 - For example, we could have a House class that looks like this: ... Abstract classes are very similar to interfaces; they're not designed to be instantiated on their own and provide a base line implementation for you to extend from.
๐ŸŒ
DEV Community
dev.to โ€บ jamir_hossain_8800f85fdd5 โ€บ php-oop-part-5-abstraction-and-interface-28ol
PHP OOP Part-5: Abstraction and Interface - DEV Community
January 2, 2025 - Now, what can we do if, after inheriting these common methods, we want them to behave differently for each class? To solve this problem, PHP provides the Abstract class.
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ base class & interfaces or abstract classes - i'm lost when to use what
r/PHPhelp on Reddit: Base Class & Interfaces or Abstract Classes - I'm lost when to use what
September 17, 2024 -

I understand a base class provides common methods to other classes that extend it.

And from what I've read, Interfaces provide a way to make sure that a method exists in your class.

It seems that Abstract class combines the two into a single file, so I'm a bit confused on when to use what.

Let's say I have a method called get_device() that is shared amount LaptopDevice, DesktopDevice, and MobileDevice classes.

Each device class gets a list of devices from its device type.

Would I do something like:

abstract class AbstractDevice {
   public function get_device() {
       return $this->device;
   }
}

class MobileDevice extends AbstractDevice {
}

or this

class Device implements DeviceInterface {
    public function get_device() {
       return $this->device;
    }
}

class MobileDevice extends Device implements DeviceInterface {
}
Top answer
1 of 5
7
Great question. There is so much confusion on the matter. First of all, technically here is no such thing in PHP as a "Base class". And Abstract class can be considered a good enough substitution, so you can think of Abstract class as a Base class. Hence we have only two matters left, Abstract class and Interface. Although they have overlapping responsibility, I devised a simple method to tell them apart: an Interface is sort of a public contract: it faces outwards. It tells other classes, what to expect from our class. Not to mention that a class could implement many Interfaces. an Abstract class is sort of a blueprint for other classes. It faces inwards. It tells other classes of the same type what they should be. And also implements the common functionality. Based on the above, this is how I do primitive class hierarchies I am able to design: decide the public contract my class has to offer, and create an Interface decide the inner workings and common functionality and create an Abstract class proceed to creating actual classes that extend the Base Abstract class (and already implements all its interfaces, though some Interfaces can be added to that particular class as well). So it could be like interface DeviceInterface { public function getDevice(); } abstract class AbstractDevice implements DeviceInterface { protected $device; public function getDevice() { return $this->device; } } class MobileDevice extends AbstractDevice { public function __construct() { $this->device = 'iPhone'; } } $device = new MobileDevice(); echo $device->getDevice();
2 of 5
5
Let's just get this out of the way first: While you technically can use inheritance (extending from another class) to share common behavior it is not typically recommended anymore to do so. That said of course if you're just going through a course the course might still go with pretty old recommendations. Instead the recommendation nowadays is to employ composition over inheritance . In practice this means that you'd be better off just implementing an interface and not even extending from anything at all in most cases. If anything base classes (abstract or not) only should (a "should" is a strong recommendation but not a hard rule) only implement methods that are the same for all subclasses. The guiding principle here is the LSP, the Liskov substitution principle (easily the most objectively quantifiable one out of SOLID ). The LSP basically says to make sure that an instance of any subclass can be used in place of the base class without problems. The most obvious violation of the LSP would be for example if you have a method in the base class and in a subclass you instead throw something like a NotImplementedException (something that can be seen in many real world projects sadly) because the subclass "doesn't use that method". It forces anyone using that base class to know about the implementation details of its subclasses which is something that would go against the goal of encapsulation. Overall these recommendations and principles make abstract classes or base classes in general a very rare breed. Well, not rare as in that you won't see it in real-world code but rare as in it probably shouldn't have been done this way. Strictly speaking base classes, abstract classes, inheritance itself is not actually necessary at all. For example instead of an abstract class Bar that wants its subclasses to implement an abstract method foo() which would then be used in a non-abstract method bar() could also be modelled as such: interface FooInterface { public function foo(); } class FooImplementation implements FooInterface { public function foo() { // ... } } class Bar { private $foo; public function __construct(FooInterface $foo) { $this->foo = $foo; } public function bar() { $this->foo->foo(); // ... } }
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ language.oop5.abstract.php
PHP: Class Abstraction - Manual
An interface is always an agreement or a promise. When a class says "I implement interface Y", it is saying "I promise to have the same public methods that any object with interface Y has". On the other hand, an Abstract Class is like a partially built class...
๐ŸŒ
Medium
rifatcse09.medium.com โ€บ abstract-vs-interfaces-classes-in-php-47067351c519
Abstract vs Interfaces Classes in PHP | by Rifat | Medium
November 3, 2020 - In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract. Method of php interface must be public only canโ€™t have access modifier by default everything assumed as public.
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ article โ€บ what-is-the-difference-between-abstract-class-and-interface-in-php
The Difference Between Abstract Class and Interface in PHP
April 20, 2020 - Interface classes completely empty the shells while expecting child classes to implement everything for them. Abstract classes not only contain the common piece of information between the shells inside but also expect the child classes within ...
๐ŸŒ
Quora
quora.com โ€บ What-is-different-between-abstract-class-and-interface-in-PHP
What is different between abstract class and interface in PHP? - Quora
Answer (1 of 4): Php abstract classes and interface are similar to like in other oops languages the main differences in programing point of view are 1. In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract. 2. Multiple and multi...
๐ŸŒ
Better Programming
betterprogramming.pub โ€บ php-a-practical-demonstration-of-interfaces-vs-abstract-classes-56d9838cd5b7
A Practical Demonstration of Interfaces vs. Abstract ...
January 24, 2020 - It provides functions that implement code that can be shared by all of the classes. Itโ€™s not specific to any database or table (especially if you remove the hard-coded credentials in dbConnect), and therefore fulfills the requirements of an abstract class in PHP. Now letโ€™s take a look at an interface.
๐ŸŒ
DEV Community
dev.to โ€บ norbybaru โ€บ how-to-use-traits-interface-and-abstract-classes-in-php-428b
How to use Traits, Interface, and Abstract classes In PHP - DEV Community
May 31, 2024 - Traits provide a way to reuse code, Interfaces define a contract that must be implemented, and Abstract Classes provide a way to define a base class with some implementation.
๐ŸŒ
Tutorials24x7
tutorials24x7.com โ€บ php โ€บ interface-vs-abstract-class-in-php
Explore the differences between PHP interfaces and abstract classes.
January 12, 2020 - 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. Notes: This tutorial assumes that you are using PHP 7.1 or above and already familiar with using Composer.
๐ŸŒ
Team Treehouse
teamtreehouse.com โ€บ library โ€บ designing-interfaces-in-php โ€บ abstract-class-vs-interface
Abstract Class vs Interface (How To) | Designing Interfaces in PHP | Treehouse
Abstract Class vs Interface. The limitation of interfaces is that they do not actually store any implementation details themselves. To share implem...
Published ย  September 6, 2017
๐ŸŒ
W3Schools
w3schools.invisionzone.com โ€บ server scripting โ€บ php
Abstract classes vs interface - PHP - W3Schools Forum
November 4, 2013 - What's the difference between abstract classes and interface in PHP ? I'm a bit confused about abstract access control...