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.

๐ŸŒ
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, ...
๐ŸŒ
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.
๐ŸŒ
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....
๐ŸŒ
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?
August 26, 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).
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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...
๐ŸŒ
Codeinphp
codeinphp.github.io โ€บ post โ€บ abstract-class-vs-interface
Abstract Class vs Interface :: Code In PHP
Interface does'n contains Constructors | Abstract class contains Constructors ยท It sounds like this point applies to may be other languages but in PHP an interface CAN have an empty constructor shell:
๐ŸŒ
PHP Classes
phpclasses.org โ€บ package โ€บ 10758-PHP-Examples-of-using-interfaces-and-abstract-classes.html
PHP Interface vs Abstract Class: Examples of using interfaces and abstract classes - PHP Classes
In a large project, if I have multiple classes and a need to change, then I have to change all the classes manually. But If we use an interface this problem is solved; and we will not need to change code manually. Now see the following code and try to realize what happened if I use interface: <?php interface Logger { public function execute($message); } class LogToDatabase implements Logger { public function execute($message){ var_dump('log the message to a database :'.$message); } } class LogToFile implements Logger { public function execute($message) { var_dump('log the message to a file :'.$message); } } class UsersController { protected $logger; public function __construct(Logger $logger) { $this->logger = $logger; } public function show() { $user = 'nahid'; $this->logger->execute($user); } } $controller = new UsersController(new LogToDatabase); $controller->show();
๐ŸŒ
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.
๐ŸŒ
Php
php.org โ€บ home โ€บ php interface vs abstract class
php interface vs abstract class
January 21, 2023 - By using the interface class, we can specify which methods a class should implement that extend the interface. Unlike abstract class, you donโ€™t need to write abstracts with the methods because an interface can only have abstract methods in it.
๐ŸŒ
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 ...
๐ŸŒ
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(); // ... } }
๐ŸŒ
Better Programming
betterprogramming.pub โ€บ php-a-practical-demonstration-of-interfaces-vs-abstract-classes-56d9838cd5b7
A Practical Demonstration of Interfaces vs. Abstract Classes in ...
December 13, 2021 - Because the interface enforces its implementation rule, if I tried to leave out one of the functions in Customers, for example insertRecord, and tried to instantiate it, we would receive a fatal error: PHP Fatal error: Class Customers contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Data_Interface::insertRecord)
๐ŸŒ
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.
๐ŸŒ
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...
๐ŸŒ
Javatpoint
javatpoint.com โ€บ php-oops-difference-between-abstract-class-and-interface
PHP OOPs Difference Between Abstract, Class and Interface - javatpoint
PHP OOPs Difference Between Abstract, Class and Interface for beginners and professionals with examples, php file, php session, php date, php array, php form, functions, time, xml, ajax, php mysql, regex, string, oop, chop(), bin2hex(), addslashes(), addcslashes() etc.