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... ?>
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! ?>
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... ?>
π― 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. ππ¨