An interface in PHP defines a contract that classes must adhere to. It specifies a set of methods that any implementing class must define, but it does not provide any implementation for these methods.
Key Features of Interfaces
Method Declaration Only:
Interfaces only declare methods. They do not contain any method implementation.
No Properties:
Interfaces cannot contain properties, unlike abstract classes.
All Methods Are Public:
Methods in an interface are implicitly public and cannot have any other access modifier.
Multiple Implementations:
A class can implement multiple interfaces, even while extending another class.
Polymorphism:
Interfaces allow for polymorphism by enabling a single piece of code to work with different classes that implement the same interface.
<?php
interface InterfaceName {
public function method1();
public function method2($param);
public function method3(): string;
}
?>
Example 1: Basic Interface Implementation
<?php
// Define the interface
interface Animal {
public function makeSound();
}
// Implement the interface in a class
class Cat implements Animal {
public function makeSound() {
echo "Meow";
}
}
// Create an object of the class
$cat = new Cat();
$cat->makeSound(); // Outputs: Meow
?>
Example 2: Polymorphism Using Interfaces
<?php
// Define the interface
interface Animal {
public function makeSound();
}
// Implement the interface in multiple classes
class Dog implements Animal {
public function makeSound() {
echo "Bark";
}
}
class Mouse implements Animal {
public function makeSound() {
echo "Squeak";
}
}
// Create a list of animals
$dog = new Dog();
$mouse = new Mouse();
$animals = [$dog, $mouse];
// Use a loop to make all animals produce their sound
foreach ($animals as $animal) {
$animal->makeSound();
echo "<br>";
}
?>
Interfaces vs. Abstract Classes
Aspect Interface Abstract Class
Properties Cannot have properties Can have properties
Methods All methods are abstract and public Can have abstract and concrete methods
Access Modifiers Methods must be public Methods can be public or protected
Multiple Inheritance A class can implement multiple interfaces A class can extend only one abstract class
Keyword interface and implements keywords are used abstract and extends keywords are used
Example 3: Implementing Multiple Interfaces
<?php
// Define interfaces
interface Writer {
public function write($content);
}
interface Reader {
public function read();
}
// Implement both interfaces in a single class
class FileManager implements Writer, Reader {
private $file;
public function __construct($fileName) {
$this->file = $fileName;
}
public function write($content) {
file_put_contents($this->file, $content);
}
public function read() {
return file_get_contents($this->file);
}
}
// Using the FileManager class
$fileManager = new FileManager("example.txt");
$fileManager->write("Hello, World!");
echo $fileManager->read(); // Outputs: Hello, World!
?>
Advantages of Using Interfaces
Flexibility:
Classes can implement multiple interfaces, allowing developers to mix functionality from various sources.
Standardization:
Interfaces define a consistent structure for implementing classes, ensuring uniformity in design.
Polymorphism:
Enables the use of different classes interchangeably when they implement the same interface.
Decoupling:
Interfaces reduce coupling between components, making the code more modular and easier to maintain.
Use Case: Dependency Injection with Interfaces
Using interfaces, you can inject dependencies that adhere to a common contract, improving flexibility and testability.
<?php
interface Logger {
public function log($message);
}
class FileLogger implements Logger {
public function log($message) {
file_put_contents("log.txt", $message . PHP_EOL, FILE_APPEND);
}
}
class App {
private $logger;
public function __construct(Logger $logger) {
$this->logger = $logger;
}
public function run() {
$this->logger->log("Application is running");
}
}
// Inject FileLogger into App
$app = new App(new FileLogger());
$app->run(); // Logs: "Application is running" in log.txt
?>
Conclusion
Interfaces are a powerful tool in PHP for defining contracts that multiple classes can adhere to. They promote polymorphism, decoupling, and consistent design, making them essential for large-scale and maintainable applications.
