Current Location: Home> Latest Articles> PHP Interfaces vs Abstract Classes: Key Differences and Usage

PHP Interfaces vs Abstract Classes: Key Differences and Usage

M66 2025-10-27

Core Differences Between PHP Interfaces and Abstract Classes

Interfaces and abstract classes are essential tools in PHP for building extensible and modular code. Interfaces enforce a method contract through implementation, while abstract classes provide partial implementations through inheritance. Interfaces cannot contain concrete methods, but abstract classes can. A class can implement multiple interfaces but can inherit from only one abstract class. Interfaces cannot be instantiated, whereas abstract classes can.

Overview of Interfaces

An interface defines a method contract, requiring any implementing class to define all methods but providing no implementation itself. Interfaces only specify method signatures, ensuring consistency across classes.

Interface Syntax Example

interface IExample {
    public function doSomething();
}

Overview of Abstract Classes

Abstract classes allow partial implementation and can include both abstract methods and concrete methods. Abstract methods must be implemented by child classes, while concrete methods provide shared functionality. Abstract classes allow child classes to extend and customize behavior.

Abstract Class Syntax Example

abstract class Example {
    public function doSomething() {
        // Concrete implementation
    }

    abstract public function doSomethingElse();
}

Key Differences Between Interfaces and Abstract Classes

  • Enforcement: Interfaces are enforced through implementation, while abstract classes are enforced through inheritance.
  • Concrete Methods: Interfaces cannot contain concrete methods, but abstract classes can.
  • Multiple Inheritance: A class can implement multiple interfaces but can only inherit from one abstract class.
  • Instantiability: Interfaces cannot be instantiated, but abstract classes can.

Practical Example

Suppose we need to create drawable shape objects. We can achieve this using either interfaces or abstract classes depending on the requirement.

Using an Interface

interface IDrawable {
    public function draw();
}

class Circle implements IDrawable {
    public function draw() {
        // Concrete implementation for drawing a circle
    }
}

class Square implements IDrawable {
    public function draw() {
        // Concrete implementation for drawing a square
    }
}

Using an Abstract Class

abstract class Shape {
    public function draw() {
        // Shared drawing implementation
    }

    abstract public function getArea();
}

class Circle extends Shape {
    public function getArea() {
        // Concrete implementation for calculating circle area
    }
}

class Square extends Shape {
    public function getArea() {
        // Concrete implementation for calculating square area
    }
}

Conclusion

Choosing between an interface and an abstract class depends on your specific needs: if only a method contract is required, an interface is suitable; if shared implementation and concrete methods are needed, an abstract class is more appropriate. Understanding these differences helps write clear and maintainable object-oriented code in PHP.