Skip to content

Instantly share code, notes, and snippets.

@nixon1333
Last active April 11, 2017 16:55
Show Gist options
  • Save nixon1333/21714e4c8616fa918fb33a5347609da1 to your computer and use it in GitHub Desktop.
Save nixon1333/21714e4c8616fa918fb33a5347609da1 to your computer and use it in GitHub Desktop.
<?php
class Automobile
{
private $vehicleMake;
private $vehicleModel;
public function __construct($make, $model)
{
$this->vehicleMake = $make;
$this->vehicleModel = $model;
}
public function getMakeAndModel()
{
return $this->vehicleMake . ' ' . $this->vehicleModel;
}
}
class AutomobileFactory
{
public static function create($make, $model)
{
return new Automobile($make, $model);
}
}
// have the factory create the Automobile object
$veyron = AutomobileFactory::create('Bugatti', 'Veyron');
print_r($veyron->getMakeAndModel()); // outputs "Bugatti Veyron"
<?php
interface OutputInterface
{
public function load();
}
class SerializedArrayOutput implements OutputInterface
{
public function load()
{
return serialize($arrayOfData);
}
}
class JsonStringOutput implements OutputInterface
{
public function load()
{
return json_encode($arrayOfData);
}
}
class ArrayOutput implements OutputInterface
{
public function load()
{
return $arrayOfData;
}
}
?>
<!-- now modify -->
<?php
class SomeClient
{
private $output;
public function setOutput(OutputInterface $outputType)
{
$this->output = $outputType;
}
public function loadOutput()
{
return $this->output->load();
}
}
?>
<!-- now call it -->
<?php
$client = new SomeClient();
// Want an array?
$client->setOutput(new ArrayOutput());
$data = $client->loadOutput();
// Want some JSON?
$client->setOutput(new JsonStringOutput());
$data = $client->loadOutput();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment