Paste_Image.png
Paste_Image.png
Paste_Image.png
<?php
interface ICanEat
{
public function eat($food);
}
class Human implements ICanEat
{
public function eat($food)
{
echo "Human eating " . $food . "<br/>";
}
}
class Animal implements ICanEat
{
public function eat($food)
{
echo "Animal eating " . $food . "<br/>";
}
}
$obj = new Human();
$obj->eat("apple");//Human eating apple
$monkey = new Animal();
$monkey->eat("Banana");//Animal eating Banana
var_dump($obj instanceof Human);//boolean true
function checkEat($obj)
{
if ($obj instanceof ICanEat) {
$obj->eat('food');
} else {
echo "The obj can't eat" . "<br/>";
}
}
checkEat($monkey);//Animal eating food
interface iCanPee extends ICanEat
{
public function pee();
}
class Human1 implements iCanPee
{
public function eat($food)
{
echo "Human1 eat " . $food . "<br/>";
}
public function pee()
{
echo "Human1 pee" . "<br/>";
}
}
?>
Paste_Image.png