0
0
PhpComparisonBeginner · 4 min read

Abstract Class vs Interface in PHP: Key Differences and Usage

In PHP, a 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.

FeatureAbstract ClassInterface
Method ImplementationCan have both abstract and concrete methodsOnly method signatures, no implementation
PropertiesCan have propertiesCannot have properties
InheritanceSupports single inheritanceSupports multiple interface implementations
Access ModifiersMethods can have any visibility (public, protected, private)All methods are implicitly public
Use CaseBase class with shared codeDefine 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
<?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();
Output
Circle area: 28.274333882308
↔️

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
<?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();
Output
Circle area: 28.274333882308
🎯

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.

Key Takeaways

Abstract classes can have implemented methods and properties; interfaces cannot.
Interfaces allow multiple implementations; abstract classes support single inheritance.
Use abstract classes to share code in a class hierarchy.
Use interfaces to define common contracts across unrelated classes.
All interface methods are public; abstract class methods can have any visibility.