PHP implements Keyword

PHP include Keyword
PHP if Keyword

In this article, you will learn how to implement polymorphism using IF statement in PHP. The implements keyword in PHP is used to declare that a class must have the methods described in the specified interface. This is called polymorphism. Polymorphism makes it easy to use a variety of different objects in the same way.

examples of the function

Example 1. In this example, we implement an interface.

<?php
interface Machine {
  public function activate();
  public function deactivate();
  public function isActive();
}

class Kettle implements Machine {
  private $isOn = false;

  public function activate() {
    $this->isOn = true;
  }

  public function deactivate() {
    $this->isOn = false;
  }

  public function isActive() {
    return $this->isOn;
  }
}

$machine = new Kettle();

$machine->activate();
if($machine->isActive()) {
  echo "The machine is on";
} else {
  echo "The machine is off";
}

echo "<br>";
$machine->deactivate();
if($machine->isActive()) {
  echo "The machine is on";
} else {
  echo "The machine is off";
}
?>
PHP include Keyword
PHP if Keyword
en English
X
Scroll to Top