Below you will find pages that utilize the taxonomy term “Builder design pattern”
Posts
Introduction to Builder design pattern in PHP
The Builder Pattern is a software design pattern which helps to simplify creation of complex objects by reducing the number of parameters in the object constructor.
This pattern uses a builder object to collect parameters of the complex object and return the final constructed object.
Problem:
Imagine we have a car object with many attributes.
The following is the class definition and object instantiation code:
<pre class="lang:php decode:true">class Car { protected $make; protected $model; protected $badge; protected $series; protected $year; protected $kilometers; protected $bodyType; protected $engineSize; protected $fuelType; protected $cylinders; protected $transmission; protected $color; public function __construct($make, $model, $badge, $series, $year, $kilometers, $bodyType, $engineSize, $fuelType, $cylinders, $transmission, $color) { $this->make = $make; $this->model = $model; $this->badge = $badge; $this->series = $series; $this->year = $year; $this->kilometers = $kilometers; $this->bodyType = $bodyType; $this->engineSize = $engineSize; $this->fuelType = $fuelType; $this->cylinders = $cylinders; $this->transmission = $transmission; $this->color = $color; } } $carObj = new car('Toyota', 'Camry', 'Altise', 'ACV36R', '2005', '60000', 'Sedan', '2400', 'Petrol', '4', 'Auto', 'White'); var_dump($carObj); As you can see everything we want to create a Car object we have to pass all those attribute to the constructor.