Google News
logo
PHP program to implement an interface into multiple classes
In the following example of PHP program to implement an interface into multiple classes :
Program :
<?php
// define the interface
interface Vehicle {
  public function start();
  public function stop();
}

// define a class that implements the interface
class Car implements Vehicle {
  public function start() {
    echo "Car started" . PHP_EOL;
  }

  public function stop() {
    echo "Car stopped" . PHP_EOL;
  }
}

// define another class that implements the interface
class Truck implements Vehicle {
  public function start() {
    echo "Truck started" . PHP_EOL;
  }

  public function stop() {
    echo "Truck stopped" . PHP_EOL;
  }
}

// create objects of the classes and call the methods
$car = new Car();
$truck = new Truck();

$car->start();
$car->stop();

$truck->start();
$truck->stop();
?>
Output :
Car started
Car stopped
Truck started
Truck stopped
In this example, we define an interface `Vehicle` with two methods `start` and `stop`. We then define two classes `Car` and `Truck` that implement the `Vehicle` interface by providing implementations for the `start` and `stop` methods.

Finally, we create objects of the `Car` and `Truck` classes and call their respective methods.