PHP Abstract Class vs Interface

PHP Abstract Class vs Interface – Key Differences & When to Use 🧐

Both abstract classes and interfaces are used to define blueprints for classes in PHP. But they have different use cases. Let’s break it down with fun and interactive examples! πŸš€


πŸ”Ή What is an Abstract Class?

An abstract class is a class that cannot be instantiated. It can have both abstract methods (without a body) and regular methods (fully implemented).

πŸ“ Example: Abstract Class in Action

<?php
abstract class Animal {
    abstract public function makeSound(); // Abstract method

    public function eat() {
        return "Eating... 🍽️";
    }
}

class Dog extends Animal {
    public function makeSound() {
        return "Woof! 🐢";
    }
}

$dog = new Dog();
echo $dog->makeSound(); // Output: Woof!
echo "<br>";
echo $dog->eat(); // Output: Eating...
?>

Try It Now

Key Features of Abstract Classes:

  • Can have both abstract (unimplemented) and regular methods.
  • Can have properties with access modifiers (public, protected, private).
  • Child classes must implement all abstract methods.
  • A class can only extend one abstract class.

πŸ”Ή What is an Interface?

An interface in PHP is like a contract that classes must follow. It only contains method declarations, and classes that implement it must define those methods.

πŸ“ Example: Interface in Action

<?php
interface Animal {
    public function makeSound();
}

class Cat implements Animal {
    public function makeSound() {
        return "Meow! 🐱";
    }
}

$cat = new Cat();
echo $cat->makeSound(); // Output: Meow!
?>

Try It Now

Key Features of Interfaces:

  • All methods must be public and cannot have implementations.
  • Cannot have properties.
  • A class can implement multiple interfaces.
  • Forces a class to follow a certain structure.

πŸ”Ή Abstract Class vs Interface: Key Differences

Feature Abstract Class Interface
Method Implementation Can have both implemented & abstract methods Cannot have implemented methods (only method declarations)
Properties Can have properties Cannot have properties
Access Modifiers Supports public, protected, private All methods are public by default
Extensibility A class can extend only one abstract class A class can implement multiple interfaces
Use Case When classes share common behavior When multiple classes should follow a structure

πŸ”Ή When to Use What?

  • Use Abstract Classes β†’ When classes share some behavior but also need custom implementations.
  • Use Interfaces β†’ When multiple unrelated classes should follow a common structure.
  • Sometimes, a mix of both is needed! πŸ’‘

πŸ“ Example: Using Both Abstract Classes & Interfaces

<?php
interface Flyable {
    public function fly();
}

abstract class Bird {
    abstract public function makeSound();
    
    public function eat() {
        return "Peck peck... 🐦";
    }
}

class Sparrow extends Bird implements Flyable {
    public function makeSound() {
        return "Chirp chirp! 🐦";
    }

    public function fly() {
        return "Flying high! ✈️";
    }
}

$sparrow = new Sparrow();
echo $sparrow->makeSound(); // Output: Chirp chirp!
echo "<br>";
echo $sparrow->fly(); // Output: Flying high!
echo "<br>";
echo $sparrow->eat(); // Output: Peck peck...
?>

Try It Now


🎯 Key Takeaways

  • Abstract Classes allow both regular and abstract methods.
  • Interfaces enforce a strict contract for classes.
  • A class can only extend one abstract class but can implement multiple interfaces.
  • Use abstract classe for shared behavior and interfaces for common structure.

πŸ“ Practice Time!

Create an interface Drivable with a method drive(), and an abstract class Vehicle with a method fuelType(). Then, create a Car class that implements the interface and extends the abstract class. πŸš—πŸ’¨