多态实例:
<?php
class Animal{
functionmakeSound(){
print "Error:this method should bere-implemented in the children";
}
}
class Cat extends Animal{
functionmakeSound(){
print "miau";
}
}
class Dog extends Animal{
functionmakeSound(){
print "wuff";
}
}
function printTheRightSound($obj){
if($obj instanceofAnimal){
$obj->makeSound();
}else{
print "Error: Passed wrong kind ofobject";
}
print "\n";
}
printTheRightSound(new Cat());
printTheRightSound(new Dog());
?>