Abstract Class vs Interface in PHP: Key Differences and Usage
abstract class can have both implemented and abstract methods and allows property declarations, while an interface only declares method signatures without implementations or properties. Abstract classes support single inheritance, but interfaces allow multiple implementations, making them ideal for defining common contracts.Quick Comparison
Here is a quick side-by-side comparison of abstract classes and interfaces in PHP.
| Feature | Abstract Class | Interface |
|---|---|---|
| Method Implementation | Can have both abstract and concrete methods | Only method signatures, no implementation |
| Properties | Can have properties | Cannot have properties |
| Inheritance | Supports single inheritance | Supports multiple interface implementations |
| Access Modifiers | Methods can have any visibility (public, protected, private) | All methods are implicitly public |
| Use Case | Base class with shared code | Define a contract for classes to implement |
Key Differences
An abstract class in PHP is a class that cannot be instantiated on its own and can contain both fully implemented methods and abstract methods that child classes must implement. It can also have properties and use any access modifiers like private or protected. This makes abstract classes useful when you want to share common code among related classes but still enforce some methods to be defined by subclasses.
On the other hand, an interface only declares method signatures without any implementation or properties. All methods in an interface are implicitly public. Interfaces allow a class to implement multiple contracts, enabling a form of multiple inheritance. This is helpful when you want to ensure that different classes follow the same set of methods regardless of their place in the class hierarchy.
In summary, abstract classes are about sharing code and structure in a class hierarchy, while interfaces are about defining capabilities that can be added to any class.
Code Comparison
This example shows how an abstract class defines a base shape with a method to calculate area, partially implemented, and a method to get the shape's name.
<?php abstract class Shape { protected string $name; public function __construct(string $name) { $this->name = $name; } abstract public function area(): float; public function getName(): string { return $this->name; } } class Circle extends Shape { private float $radius; public function __construct(float $radius) { parent::__construct('Circle'); $this->radius = $radius; } public function area(): float { return pi() * $this->radius * $this->radius; } } $circle = new Circle(3); echo $circle->getName() . ' area: ' . $circle->area();
Interface Equivalent
This example shows how an interface defines the same contract for shapes, requiring methods to get the name and calculate the area, but without any shared code or properties.
<?php
interface ShapeInterface {
public function getName(): string;
public function area(): float;
}
class Circle implements ShapeInterface {
private float $radius;
public function __construct(float $radius) {
$this->radius = $radius;
}
public function getName(): string {
return 'Circle';
}
public function area(): float {
return pi() * $this->radius * $this->radius;
}
}
$circle = new Circle(3);
echo $circle->getName() . ' area: ' . $circle->area();
When to Use Which
Choose an abstract class when you want to share common code, properties, or behavior among related classes and enforce some methods to be implemented. It works well for a clear class hierarchy where subclasses extend a base class.
Choose an interface when you want to define a contract that multiple unrelated classes can implement, especially if they already extend other classes. Interfaces are best for defining capabilities or roles that can be added to any class.