In PHP, an interface is a blueprint for creating classes. It defines a set of methods that any class implementing the interface must define.
Interfaces provide a way to enforce consistent method names across different classes, making them essential for designing large-scale applications that need to adhere to a specific contract or behavior.
In this tutorial, we will cover:
Table of Contents
Let’s explore each of these topics with detailed explanations and examples.
1. What is an Interface?
An interface in PHP is a contract that defines a set of methods that a class must implement. Interfaces only define method signatures (i.e., the method names and parameters), but they do not provide any implementation.
A class that implements an interface must provide the actual implementation for all the methods declared in the interface.
Key points about interfaces:
- An interface cannot contain properties, only method declarations.
- All methods declared in an interface must be public.
- A class can implement multiple interfaces.
2. Defining an Interface
You define an interface in PHP using the interface keyword. Inside the interface, you declare method signatures without defining the method bodies.
Syntax:
interface InterfaceName { public function methodName(); }
Example (Defining an Interface):
<?php // Define an interface interface Animal { public function makeSound(); } ?>
- In this example, the Animal interface defines a method makeSound(). Any class that implements this interface must provide an implementation for makeSound().
3. Implementing an Interface
When a class implements an interface, it must define all the methods declared in the interface. This ensures that the class adheres to the contract defined by the interface.
Example (Implementing an Interface):
<?php // Define the Animal interface interface Animal { public function makeSound(); } // Implement the interface in a class class Dog implements Animal { public function makeSound() { echo "Bark\n"; } } // Implement the interface in another class class Cat implements Animal { public function makeSound() { echo "Meow\n"; } } // Instantiate objects and call the method $dog = new Dog(); $dog->makeSound(); // Outputs: Bark $cat = new Cat(); $cat->makeSound(); // Outputs: Meow ?>
- In this example, both Dog and Cat classes implement the Animal interface and provide their own implementation of the makeSound() method.
4. Multiple Interfaces
A class in PHP can implement multiple interfaces. This allows the class to inherit behavior from multiple sources and combine functionality.
Example (Implementing Multiple Interfaces):
<?php // Define two interfaces interface Flyable { public function fly(); } interface Swimmable { public function swim(); } // Implement both interfaces in a class class Duck implements Flyable, Swimmable { public function fly() { echo "The duck flies\n"; } public function swim() { echo "The duck swims\n"; } } // Instantiate the Duck class and call both methods $duck = new Duck(); $duck->fly(); // Outputs: The duck flies $duck->swim(); // Outputs: The duck swims ?>
- In this example, the Duck class implements both Flyable and Swimmable interfaces, meaning the class must provide implementations for both fly() and swim() methods.
5. Interface Inheritance
Just like classes, interfaces can also inherit from other interfaces. When an interface inherits from another, the child interface inherits all the method signatures from the parent interface.
Example (Interface Inheritance):
<?php // Define a parent interface interface Animal { public function eat(); } // Define a child interface that extends the parent interface interface Mammal extends Animal { public function walk(); } // Implement the child interface in a class class Human implements Mammal { public function eat() { echo "Human eats food\n"; } public function walk() { echo "Human walks\n"; } } // Instantiate the class and call its methods $person = new Human(); $person->eat(); // Outputs: Human eats food $person->walk(); // Outputs: Human walks ?>
- The Mammal interface extends the Animal interface, so any class that implements Mammal must provide implementations for both eat() and walk() methods.
6. Difference Between Interfaces and Abstract Classes
While both interfaces and abstract classes provide a way to define methods that must be implemented by child classes, there are key differences between them:
Aspect | Interfaces | Abstract Classes |
---|---|---|
Method Implementation | Cannot contain method implementations, only declarations | Can have both method declarations and implementations |
Properties | Cannot contain properties | Can contain properties |
Multiple Inheritance | A class can implement multiple interfaces | A class can extend only one abstract class |
Constructor | Cannot have a constructor | Can have a constructor |
Example (Interface vs. Abstract Class):
<?php // Define an abstract class abstract class Animal { abstract public function makeSound(); public function sleep() { echo "Sleeping...\n"; } } // Define an interface interface Flyable { public function fly(); } // A class can extend an abstract class and implement an interface class Bird extends Animal implements Flyable { public function makeSound() { echo "Chirp chirp\n"; } public function fly() { echo "The bird is flying\n"; } } $bird = new Bird(); $bird->makeSound(); // Outputs: Chirp chirp $bird->fly(); // Outputs: The bird is flying $bird->sleep(); // Outputs: Sleeping... ?>
- The Bird class extends the abstract class Animal and implements the Flyable interface, combining the behavior of both.
7. Example Use Case: Payment System
Interfaces are often used to define a contract for different implementations in a system. For example, in a payment system, you might have multiple payment gateways (PayPal, Stripe, etc.), but all of them should follow the same method structure.
Example (Payment System):
<?php // Define a PaymentProcessor interface interface PaymentProcessor { public function processPayment($amount); } // Implement the PaymentProcessor interface for PayPal class PayPalProcessor implements PaymentProcessor { public function processPayment($amount) { echo "Processing $" . $amount . " via PayPal\n"; } } // Implement the PaymentProcessor interface for Stripe class StripeProcessor implements PaymentProcessor { public function processPayment($amount) { echo "Processing $" . $amount . " via Stripe\n"; } } // Use the classes $paypal = new PayPalProcessor(); $paypal->processPayment(100); // Outputs: Processing $100 via PayPal $stripe = new StripeProcessor(); $stripe->processPayment(150); // Outputs: Processing $150 via Stripe ?>
- In this use case, the PaymentProcessor interface ensures that all payment processors (PayPal, Stripe, etc.) implement the processPayment() method, making it easier to switch between different payment gateways in the future.
Summary of PHP Interfaces:
Concept | Description |
---|---|
interface | Defines a set of method signatures that a class must implement |
implements | Used by a class to implement an interface |
Multiple Interfaces | A class can implement multiple interfaces |
Interface Inheritance | Interfaces can extend other interfaces |
public | All methods in an interface must be public |
Interface vs. Abstract | Interfaces cannot have method implementations or properties, unlike abstract classes |
Conclusion
Interfaces in PHP are a key feature for ensuring that classes follow a specific contract, allowing you to build scalable and maintainable applications. In this tutorial, we covered:
- Defining and implementing interfaces.
- Using multiple interfaces and interface inheritance to extend functionality.
- Differences between interfaces and abstract classes.
- A real-world use case (payment system) to demonstrate the practical application of interfaces.