Skip to content

Instantly share code, notes, and snippets.

@whizark
Last active August 29, 2015 14:03
Show Gist options
  • Save whizark/e614eaf67b33a798b876 to your computer and use it in GitHub Desktop.
Save whizark/e614eaf67b33a798b876 to your computer and use it in GitHub Desktop.
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment