Let's play with the the previous (if trivial) example, this time using an interface:
<?php
abstract class Weapon {
protected $name;
public function __construct($name)
{
$this->name = $name;
}
}
interface RangedWeapon
{
public function load();
public function shoot();
}
class Bow extends Weapon implements RangedWeapon
{
public function load()
{
echo $this->name . ": Take arrow from quiver and nock it on bowstring";
}
public function shoot()
{
echo $this->name . ": Pull string back, aim, and release";
}
}
class AssaultRifle extends Weapon implements RangedWeapon
{
public function load()
{
echo $this->name . ": Remove empty magazine, insert full magazine.";
}
public function shoot()
{
echo $this->name . ": Aim, squeeze trigger";
}
}
$test = new Bow('longbow');
$test->load();
echo "<br />";
$test->shoot();
echo "<br />";
$test = new AssaultRifle('AK47');
$test->load();
echo "<br />";
$test->shoot();
Output:
longbow: Take arrow from quiver and nock it on bowstring
longbow: Pull string back, aim, and release
AK47: Remove empty magazine, insert full magazine.
AK47: Aim, squeeze trigger
Note that any place you might use type hinting when passing one of our objects, a Bow object, for example, would qualify as "Bow", "RangedWeapon", and "Weapon", as it qualifies as itself, any class it inherits, and any interface it implements or inherits.