Skip to content

Instantly share code, notes, and snippets.

@vranac vranac/better-example.php
Last active Aug 29, 2015

Embed
What would you like to do?
<?php
interface iPropulsion
{
public function accelerate();
public function decelerate();
}
interface iOrdonance
{
public function lockOn();
public function fire();
}
class CarEngine implements iPropulsion
{
public function accelerate()
{
return "increase revs to 3000... This should shake yer kidney stones...";
}
public function decelerate()
{
return "decrease revs to 1000";
}
}
class JetEngine implements iPropulsion
{
public function accelerate()
{
return "increase revs to 20000... To the ends of skies...";
}
public function decelerate()
{
return "decrease revs to 5000";
}
}
class Cannon implements iOrdonance
{
public function lockOn()
{
return "Srsly?! Just point and shoot, how hard is that...";
}
public function Fire()
{
return "Dagagagagaga!";
}
}
class Rocket implements iOrdonance
{
public function lockOn()
{
return "Beep... Beep... Beeeeeeeeeeeep...";
}
public function Fire()
{
return "Swhooosh! Ka-Boom!";
}
}
class Vehicle {
private $propulsion = null;
private $ordonance = null;
public function __construct(iPropulsion $propulsion, iOrdonance $ordonance)
{
$this->propulsion = $propulsion;
$this->ordonance = $ordonance;
}
public function start()
{
print $this->propulsion->accelerate() . PHP_EOL;
}
public function stop()
{
print $this->propulsion->decelerate() . PHP_EOL;
}
public function lockOn()
{
print $this->ordonance->lockOn() . PHP_EOL;
}
public function fire()
{
print $this->ordonance->fire() . PHP_EOL;
}
public function setPropulsion(iPropulsion $propulsion)
{
$this->propulsion = $propulsion;
}
public function setOrdonance(iOrdonance $ordonance)
{
$this->ordonance = $ordonance;
}
}
print "Hey I want a new fighting vehicle... With a Cannon!" . PHP_EOL;
$fighter = new Vehicle(new CarEngine(), new Cannon());
print "Peel off" . PHP_EOL;
$fighter->start();
print "Hey I want some target practice :)" . PHP_EOL;
print "Going to lock on the target" . PHP_EOL;
$fighter->lockOn();
print "Time to blow stuff up" . PHP_EOL;
$fighter->fire();
print "This sux, moving too slow :(" . PHP_EOL;
$fighter->stop();
print "I need a better engine, mekkaniku-san, and rockets, lotsalotsa rockets, kthxbai" . PHP_EOL;
$fighter->setPropulsion(new JetEngine());
$fighter->setOrdonance(new Rocket());
print "Once again into the fray" . PHP_EOL;
$fighter->start();
print "Going to lock on the target, hopefully this will go better than last time" . PHP_EOL;
$fighter->lockOn();
print "Yeeehaw" . PHP_EOL;
$fighter->fire();
print "Control, one bogey down" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.