Skip to main content

PHP OOP Interfaces

By SamK
0
0 recommends
Category(s)
Topic(s)

Using interfaces ensures consistency in method implementation across different classes, making it easier to work with a variety of classes in a unified way. Hence, multiple classes can be used interchangeably as long as they adhere to the same interface.

Syntax: Interfaces are declared using the interface keyword.

<?php
interface AnimalActions {
    public function makeSound();
    public function move($speed, $direction);
    public function getDetails() : string;
}
?>

Difference between Interfaces and Abstract Classes

Interfaces and abstract classes share similarities but have key differences:

  1. No Properties in Interfaces:
    Interfaces cannot define properties, while abstract classes can.
  2. Method Access Modifiers:
    All methods in an interface are inherently public, whereas abstract class methods can be public or protected.
  3. Fully Abstract Methods in Interfaces:
    Every method in an interface is implicitly abstract, so the abstract keyword is unnecessary, and no method can have a body.
  4. Multiple Inheritance Flexibility:
    A class can implement multiple interfaces while simultaneously extending a single class. 

Using Interfaces

To use an interface in a class, the implements keyword is required.

When a class implements an interface, it must provide concrete definitions for all methods declared in the interface.

Example:

<?php
// Interface definition
interface Vehicle {
    public function startEngine();
}

// Class definitions
class Car implements Vehicle {
    public function startEngine() {
        echo "Engine started";
    }
}

$vehicle = new Car();
$vehicle->startEngine();

/*
Output:
Engine started
*/
?>

Now, let's say we want to create a software to manage a fleet of vehicles. There are actions all vehicles can perform, but each type of vehicle does it in its unique way.

By using interfaces, we can write code that works for all types of vehicles, regardless of how each one behaves.

Example:

<?php
// Interface definition
interface Vehicle {
  public function startEngine();
}

// Class definitions
class Car implements Vehicle {
  public function startEngine() {
    echo "Vroom Vroom (Car engine started)" . "<br>";
  }
}

class Motorcycle implements Vehicle {
  public function startEngine() {
    echo "Rev Rev (Motorcycle engine started)" . "<br>";
  }
}

class Truck implements Vehicle {
  public function startEngine() {
    echo "Rumble Rumble (Truck engine started)" . "<br>";
  }
}

// Create a list of vehicles
$car = new Car();
$motorcycle = new Motorcycle();
$truck = new Truck();
$vehicles = array($car, $motorcycle, $truck);

// Tell the vehicles to start their engines
foreach($vehicles as $vehicle) {
  $vehicle->startEngine();
}

/*
Output:
Vroom Vroom (Car engine started)
Rev Rev (Motorcycle engine started)
Rumble Rumble (Truck engine started)
*/
?>

Car, Motorcycle, and Truck are all classes that implement the Vehicle interface, meaning each of them can start their engine using the startEngine() method. As a result, we can loop through all of the vehicles and tell them to start their engine, even if we don’t know the specific type of each vehicle.

Since the interface does not specify how the method should be implemented, each vehicle can start its engine in its own way.

Questions & Answers