🐘 PHP Intermediate

What is the difference between abstract methods and interface methods in PHP?

Answer

Both abstract methods (in abstract classes) and interface methods define a contract that implementing/extending classes must fulfill, but they exist in different contexts. Abstract methods are declared in an abstract class with the abstract keyword and can be public or protected. The abstract class may also contain concrete methods, state (instance variables), and a constructor. Interface methods are implicitly public and abstract — they define what must be done without any implementation context. A class can only extend one abstract class (single inheritance) but implement multiple interfaces. Use abstract classes when subclasses share state or partial implementation; use interfaces to define capabilities for unrelated classes. Since PHP 8.0, interfaces can also have constants.