PHP implements Keyword
Example
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";
}
?>
Try it Yourself »
Definition and Usage
The implements
keyword 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.
Related Pages
The interface
keyword
The else
keyword
Read more about objects, classes and interfaces in our PHP OOP Tutorial.
❮ PHP Keywords