Interface and Trait #test #php
<?php
interface FlyableThingInterface
{
public function fly();
}
interface AnimalInterface
{
public function eat();
}
interface FlyingAnimalInterface extends AnimalInterface, FlyableThingInterface
{
}
interface AirplaneInterface extends FlyableThingInterface
{
}
trait HerbivoreTrait
{
public function eat()
{
echo 'The herbivore eats plant material.' . PHP_EOL;
}
}
trait CarnivoreTrait
{
public function eat()
{
echo 'The carnivore eats meat.' . PHP_EOL;
}
}
trait OmnivoreTrait
{
public function eat()
{
echo 'The omnivore eats plant material and meat.' . PHP_EOL;
}
}
trait FlyingAnimalTrait
{
public function fly()
{
echo 'The animal flies.' . PHP_EOL;
}
}
trait AirplaneTrait
{
public function fly()
{
echo 'The airplane flies.' . PHP_EOL;
}
}
class Lion implements AnimalInterface
{
use CarnivoreTrait;
}
class Elephant implements AnimalInterface
{
use HerbivoreTrait;
}
class Bird implements FlyingAnimalInterface
{
use OmnivoreTrait;
use FlyingAnimalTrait;
}
class JumboJet implements AirplaneInterface
{
use AirplaneTrait;
}
$aLion = new Lion();
$anElephant = new Elephant();
$aBird = new Bird();
$aJumboJet = new JumboJet();
$aLion->eat();
// The carnivore eats meat.
$anElephant->eat();
// The herbivore eats plant material.
$aBird->eat();
// The omnivore eats plant material and meat.
$aBird->fly();
// The animal flies.
$aJumboJet->fly();
// The airplane flies.