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! ?>
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 ?>
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."); ?>
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. πποΈ