Skip to content

Instantly share code, notes, and snippets.

@Raensul
Last active January 14, 2018 19:41
Show Gist options
  • Save Raensul/a8f615d8489b7c4e5e5328810d60a52b to your computer and use it in GitHub Desktop.
Save Raensul/a8f615d8489b7c4e5e5328810d60a52b to your computer and use it in GitHub Desktop.
<?php
error_reporting(E_ALL);
/*start Classic simple builder*/
class Car
{
/* public $color = 'grey';
public $engine = 'TS';
public $transmission = 'MANUAL';
public $wheelsDrive = 2;*/
public function __construct(/*$c, $e, $t, $w*/)
{
/*$this->color = $c;
$this->engine = $e;
$this->transmission = $t;
$this->wheelsDrive = $w;*/
}
}
class CarConstruct
{
public $color = 'grey';
public $engine = 'TS';
public $transmission = 'MANUAL';
public $wheelsDrive = 2;
public function setColor($color)
{
$this->color = $color;
return $this;
}
public function setEngine($engine)
{
$this->engine = $engine;
return $this;
}
public function setTransmission($transmission)
{
$this->transmission = $transmission;
return $this;
}
public function setWheelsDrive($wheelsDrive)
{
$this->wheelsDrive = $wheelsDrive;
return $this;
}
public function buildCar()
{
return new Car($this->color,
$this->engine,
$this->transmission,
$this->wheelsDrive
);
}
}
$car1 = new CarConstruct();
$car1->setTransmission('AUTO')->setEngine('TD')->buildCar();
echo '<h3>Car 1</h3>';
echo $car1->color . '<br>';
echo $car1->wheelsDrive . '<br>';
echo $car1->transmission . '<br>';
echo $car1->engine . '<br>';
$car2 = new CarConstruct();
$car2->setColor('red')->setWheelsDrive(4);
echo '<h3>Car 2</h3>';
echo $car2->color . '<br>';
echo $car2->wheelsDrive . '<br>';
echo $car2->transmission . '<br>';
echo $car2->engine . '<br>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment