- Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.7
<?php
/**
* An example of duck typing in PHP
*/
interface CanFly {
public function fly();
}
interface CanSwim {
public function swim();
}
class Bird {
public function info() {
echo "I am a {$this->name}\n";
echo "I am an bird\n";
}
}
/**
* some implementations of birds
*/
class Dove extends Bird implements CanFly {
var $name = "Dove";
public function fly() {
echo "I fly\n";
}
}
class Penguin extends Bird implements CanSwim {
var $name = "Penguin";
public function swim() {
echo "I swim\n";
}
}
class Duck extends Bird implements CanFly, CanSwim {
var $name = "Duck";
public function fly() {
echo "I fly\n";
}
public function swim() {
echo "I swim\n";
}
}
/**
* a simple function to describe a bird
*/
function describe($bird) {
if ($bird instanceof Bird) {
$bird->info();
if ($bird instanceof CanFly) {
$bird->fly();
}
if ($bird instanceof CanSwim) {
$bird->swim();
}
} else {
die("This is not a bird. I cannot describe it.");
}
}
?>