WikiGalaxy

Personalize

PHP Interfaces

What is a PHP Interface?

A PHP interface allows you to specify what methods a class should implement. Interfaces make it easy to use a variety of classes in the same way, ensuring that they all implement the same methods.

Defining an Interface

An interface is defined using the interface keyword. All methods declared in an interface must be public.


interface Animal {
  public function makeSound();
}
    

Implementing an Interface

A class implements an interface by using the implements keyword. The class must define all the interface's methods.


class Dog implements Animal {
  public function makeSound() {
    echo "Bark";
  }
}
    

Multiple Interfaces

A class can implement multiple interfaces, separating them with commas. This allows for flexible and reusable code structures.


interface CanFly {
  public function fly();
}

class Bird implements Animal, CanFly {
  public function makeSound() {
    echo "Chirp";
  }
  
  public function fly() {
    echo "Flying";
  }
}
    

Interface Inheritance

Interfaces can extend other interfaces, similar to class inheritance. This allows interfaces to be more modular and specific.


interface Mammal extends Animal {
  public function walk();
}

class Human implements Mammal {
  public function makeSound() {
    echo "Speak";
  }
  
  public function walk() {
    echo "Walking";
  }
}
    

Using Interfaces with Type Hinting

Interfaces can be used as type hints in function definitions, ensuring that the passed objects implement the required interface.


function makeAnimalSound(Animal $animal) {
  $animal->makeSound();
}

$dog = new Dog();
makeAnimalSound($dog);
    

Abstract Classes vs Interfaces

While both abstract classes and interfaces can enforce method implementation, interfaces cannot have properties or method implementations, offering more flexibility in design.


abstract class AbstractAnimal {
  abstract public function makeSound();
}

class Cat extends AbstractAnimal {
  public function makeSound() {
    echo "Meow";
  }
}
    

Interfaces and Dependency Injection

Using interfaces with dependency injection allows for more flexible and testable code, as dependencies can be swapped easily.


class Zoo {
  private $animal;

  public function __construct(Animal $animal) {
    $this->animal = $animal;
  }

  public function hearAnimal() {
    $this->animal->makeSound();
  }
}

$bird = new Bird();
$zoo = new Zoo($bird);
$zoo->hearAnimal();
    

Interfaces in Large Applications

In large applications, interfaces help in defining clear contracts between different parts of the application, making the codebase more maintainable.


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

class FileLogger implements Logger {
  public function log($message) {
    file_put_contents('log.txt', $message, FILE_APPEND);
  }
}

class Application {
  private $logger;

  public function __construct(Logger $logger) {
    $this->logger = $logger;
  }

  public function run() {
    $this->logger->log("Application started");
  }
}

$logger = new FileLogger();
$app = new Application($logger);
$app->run();
    

Real-world Example: Payment Gateway

Interfaces are often used in real-world applications like payment gateways, where different payment methods can implement a common interface.


interface PaymentGateway {
  public function pay($amount);
}

class PayPal implements PaymentGateway {
  public function pay($amount) {
    echo "Paying $amount using PayPal";
  }
}

class Stripe implements PaymentGateway {
  public function pay($amount) {
    echo "Paying $amount using Stripe";
  }
}

function processPayment(PaymentGateway $gateway, $amount) {
  $gateway->pay($amount);
}

$paypal = new PayPal();
processPayment($paypal, 100);
    
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025