Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Factory and DI example
<?php
class Car
{
protected $power;
protected $doors;
public static function factory($type, Engine $engine, Transmission $transmission)
{
$instance = null;
switch ($type) {
case 2:
$instance = new Sedan($engine, $transmission);
break;
case 4:
$instance = new Crossover($engine, $transmission);
break;
default:
throw new Exception('Invalid type');
}
return $instance;
}
public function __construct(Engine $engine, Transmission $transmission)
{
$this->engine = $engine;
$this->transmission = $transmission;
}
}
class Sedan extends Car
{
// implementation
}
class Crossover extends Car
{
// implementation
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.