PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
Interfaces in PHP are defined using the interface
keyword and can contain public method declarations and constant definitions. These interfaces cannot contain properties or method bodies (implementations). When a class implements an interface, it inherits the abstract methods from the interface.
Here's a simple example of an interface:
interface Animal { public function makeSound(); }
To implement an interface in a class, you use the implements
keyword:
class Dog implements Animal { public function makeSound() { echo "Woof!\n"; } } class Cat implements Animal { public function makeSound() { echo "Meow!\n"; } }
In the above example, both Dog
and Cat
classes implement the Animal
interface, therefore they must provide an implementation for the makeSound
method.
You can use these classes like this:
$dog = new Dog(); $dog->makeSound(); // Outputs: Woof! $cat = new Cat(); $cat->makeSound(); // Outputs: Meow!
One class can implement multiple interfaces. Here's how:
interface Animal { public function makeSound(); } interface Movable { public function move(); } class Dog implements Animal, Movable { public function makeSound() { echo "Woof!\n"; } public function move() { echo "The dog moves forward.\n"; } }
In this example, the Dog
class implements both Animal
and Movable
interfaces, therefore it must provide an implementation for both makeSound
and move
methods.
Interfaces are used to ensure that certain classes contain certain methods. It's a way of setting up a contract that the programmer must follow. If a class implements an interface but doesn't provide implementations for all methods in the interface, PHP will throw a fatal error.
Declaring and implementing interfaces in PHP:
<?php // Declare an interface interface Logger { public function log($message); } // Implement the interface in a class class FileLogger implements Logger { public function log($message) { // Log message to a file file_put_contents('log.txt', $message, FILE_APPEND); } } // Create instance and use the interface method $fileLogger = new FileLogger(); $fileLogger->log('Log this message.');
Using interfaces for multiple inheritance in PHP:
<?php // Interface 1 interface Logger { public function log($message); } // Interface 2 interface Notifier { public function notify($message); } // Implementing both interfaces in a class class LogManager implements Logger, Notifier { public function log($message) { // Log message to a file file_put_contents('log.txt', $message, FILE_APPEND); } public function notify($message) { // Send notification echo 'Notification: ' . $message; } } // Create instance and use the interfaces methods $logManager = new LogManager(); $logManager->log('Log this message.'); $logManager->notify('Notify this message.');
Abstract classes vs. interfaces in PHP:
<?php // Abstract class abstract class AbstractLogger { abstract public function log($message); } // Interface interface Logger { public function log($message); } // Implementing the interface in a class class FileLogger implements Logger { public function log($message) { // Log message to a file file_put_contents('log.txt', $message, FILE_APPEND); } }
Interface methods and their implementation in PHP:
<?php // Interface with methods interface Shape { public function draw(); } // Implementing the interface in a class class Circle implements Shape { public function draw() { echo 'Drawing a circle'; } } // Create instance and use the interface method $circle = new Circle(); $circle->draw(); // Output: Drawing a circle
Extending interfaces and interface inheritance in PHP:
<?php // Base interface interface Logger { public function log($message); } // Extended interface interface Notifier extends Logger { public function notify($message); } // Implementing the extended interface in a class class LogManager implements Notifier { public function log($message) { // Log message to a file file_put_contents('log.txt', $message, FILE_APPEND); } public function notify($message) { // Send notification echo 'Notification: ' . $message; } } // Create instance and use the extended interface methods $logManager = new LogManager(); $logManager->log('Log this message.'); $logManager->notify('Notify this message.');
Constants in PHP interfaces:
<?php // Interface with a constant interface Status { const SUCCESS = 'success'; const ERROR = 'error'; } // Accessing the interface constant echo Status::SUCCESS; // Output: success