Builder in PHP

In the PHP world, the Builder Pattern is well-known and used in many PHP frameworks like CodeIgniter and Laravel.

The PHP Builder Design Pattern is a creational design pattern that could build specific class objects with possibilities to set properties and objects dynamically. It is instrumental when creating an object with many possible configuration options.

In the Builder Pattern, a director and a builder work together to build an object. The director controls the building and specifies the parts and variations of an object. The builder knows how to assemble the given object specification.

The Builder Pattern creates complex systems with simple objects and a step-by-step approach. Software patterns separate complex objects from their representations so that the same construction process can make different representations. The Builder Pattern is a way to untangle how to create an object from the object class itself.

Here is an example of PHP code programming with an already implemented builder design pattern.

CAR Class

class Car {
    
    private $brand;
    private $machineCapacity;
    private $machineType;
    private $mileage;

    public function __construct($brand, $machineCapacity,$machineType, $mileage){
        $this->brand = $brand;
        $this->machineCapacity = $machineCapacity;
        $this->machineType = $machineType;
        $this->mileage = $mileage;
    }

    public function getMileAge(){
        return $this->mileage;
    }

    public function getmachineCapacity(){
        return $this->machineCapacity;
    }

    public function getMachineType(){
        return $this->machineType;
    }

    public function getBrand(){
        return $this->brand;
    }

    public function setBrand($brand){
        if(null !== $brand){
            $this->brand = $brand;
            return "Brand Saved Succesfully";
        }else{
            return "You need to enter the brand name";
        }
    }

    public function addMileage($mileage){
        $this->mileage += $mileage;
    }

}

CarBuilderClass

include('Car.php');

class CarBuilder {

    private $brand = 'Unknown';
    private $machineCapacity = 'Unknown';
    private $machineType = 'Unknown';
    private $mileage = 0 ;
 
    public function setBrand($brand){
        $this->brand = $brand;
        return $this;
    }

    public function setMachineCapacity($capacity){
        $this->machineCapacity = $capacity;
        return $this;
    }

    public function setMachineType($type){
        $this->machineType = $type;
        return $this;
    }

    public function setmileAge($mile){
        $this->mileage = $mile;
        return $this;
    }

    public function build():Car {
        $car = new Car($this->brand,$this->machineCapacity,$this->machineType,$this->mileage);
        return $car;
    }

}

The Index File ( Director )

include('Carbuilder.php');

$cars = array();

$avansa = (new CarBuilder())
    ->setBrand('Toyota')
    ->setMachineType('Petrol')
    ->setMileage(8000)
    ->build();

array_push($cars,$avansa);

$fuso = (new CarBuilder())
    ->setBrand('Mithsubishi')
    ->setMachineType('Diesel')
    ->setMachineCapacity(2000)
    ->build(); 

array_push($cars,$fuso); 

 

0 ( 0 ) Comments