PHP Interfaces

PHP Interfaces – Define Structure Without Implementation πŸš€

An interface in PHP defines a blueprint for classes without providing implementation. It ensures that all classes following the interface have the required methods.

Interfaces are defined using the interface keyword and implemented using implements.


πŸ”Ή Why Use Interfaces?

  • Forces classes to follow a structure.
  • Allows multiple class implementations (unlike inheritance which allows only one parent).
  • Promotes cleaner and modular code.

πŸ“ Example 1: Defining & Implementing an Interface

Here, we create an interface Animal with a required method makeSound(). Any class implementing this interface must define the method.

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

// Implementing the interface in classes
class Dog implements Animal {
    public function makeSound() {
        return "Woof! Woof! 🐢";
    }
}

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

// Using the classes
$dog = new Dog();
$cat = new Cat();

echo $dog->makeSound(); // Output: Woof! Woof!
echo "<br>";
echo $cat->makeSound(); // Output: Meow!
?>

Try It Now

Both Dog and Cat classes implement the Animal interface and define the makeSound() method.


πŸ”Ή Multiple Interfaces

A class can implement multiple interfaces, allowing flexibility.

πŸ“ Example 2: Implementing Multiple Interfaces

<?php
// Defining multiple interfaces
interface Flyable {
    public function fly();
}

interface Speakable {
    public function speak();
}

// Implementing both interfaces
class Parrot implements Flyable, Speakable {
    public function fly() {
        return "Flapping wings... 🦜";
    }

    public function speak() {
        return "Hello! Polly wants a cracker! 🦜";
    }
}

$parrot = new Parrot();
echo $parrot->fly(); // βœ… Implements Flyable
echo "<br>";
echo $parrot->speak(); // βœ… Implements Speakable
?>

Try It Now

Here, the Parrot class implements both Flyable and Speakable interfaces.


πŸ”Ή Using Interfaces for Type Hinting

Interfaces help enforce method usage when passing objects as arguments.

πŸ“ Example 3: Interface for Type Hinting

<?php
interface Logger {
    public function log($message);
}

class FileLogger implements Logger {
    public function log($message) {
        return "Logging to file: " . $message;
    }
}

class DatabaseLogger implements Logger {
    public function log($message) {
        return "Logging to database: " . $message;
    }
}

// Function with interface type hinting
function sendLog(Logger $logger, $message) {
    return $logger->log($message);
}

$fileLogger = new FileLogger();
$dbLogger = new DatabaseLogger();

echo sendLog($fileLogger, "File system issue.");
echo "<br>";
echo sendLog($dbLogger, "Database connection lost.");
?>

Try It Now

The sendLog() function accepts any object that implements the Logger interface.


🎯 Key Takeaways

  • interface β†’ Defines required methods without implementation.
  • Classes must implement all methods in an interface.
  • Classes can implement multiple interfaces.
  • Interfaces allow for better organization and maintainability.

πŸ“ Practice Time!

Create an interface Driveable with a method drive(). Then, make Car and Bike classes implement it. πŸš—πŸοΈ