# Design patterns: Builder

> The Builder design pattern with examples in PHP

- URL: https://mautoblog.com/en/posts/design-patterns/builder/
- Language: en
- Published: 2024-09-27
- Updated: 2026-10-14
- Author: Mauto
- Categories: Software Development, Technology
- Tags: Software Architecture, Best Practices, PHP
- Guide: Design Patterns (part 5)
- Translation (it): https://mautoblog.com/posts/design-patterns/builder/

---
The Builder Pattern is ideal when you need to construct complex objects in a clear and flexible way.
It keeps the construction details separate from the final representation and improves code readability and maintainability.

## Characteristics of the Builder pattern
1. **Separation of construction and representation:** The Builder Pattern separates the process of constructing an object from its final representation, letting you create different kinds of objects with the same process.
2. **Step-by-step construction:** Objects can be built incrementally, configuring the desired parameters over several steps. Each step can return the builder itself, enabling a fluent syntax.
3. **Immutability of the final object:** The builder's final result is often immutable, ensuring it can't be modified after creation, which improves consistency and stability.
4. **Flexibility and extensibility:** You can extend the builder to support new kinds of objects without modifying existing code. Adding new methods to configure extra parameters is straightforward.
5. **Ease of use:** The Builder Pattern simplifies the creation of complex objects, hiding implementation details and reducing the number of constructors you need.
6. **Clarity and maintainability:** Creating objects with a builder is clearer and easier to understand than using complex constructors, improving code readability and maintainability.


### Example in PHP:
A common example of the Builder Pattern is constructing a Pizza object. You can have a pizza builder that lets you specify the toppings and the size.
You can add toppings or configure the pizza step by step and, once it's done, get the final Pizza object.
The builder collects the options and creates the pizza only in `build()`; the pizza has no methods to modify it, so the final object is immutable.

```php
class Pizza {
    // Readonly properties: once created, the pizza can't be modified
    public function __construct(
        private readonly string $size,
        private readonly bool $cheese,
        private readonly bool $pepperoni,
        private readonly bool $mushrooms
    ) {}

    public function __toString() {
        $toppings = [];
        if ($this->cheese) $toppings[] = "cheese";
        if ($this->pepperoni) $toppings[] = "pepperoni";
        if ($this->mushrooms) $toppings[] = "mushrooms";

        return "Pizza: Size - {$this->size}, Toppings - " . implode(", ", $toppings);
    }
}

class PizzaBuilder {
    // The builder collects the options, the pizza doesn't exist yet
    private $size;
    private $cheese = false;
    private $pepperoni = false;
    private $mushrooms = false;

    public function __construct($size) {
        $this->size = $size;
    }

    public function withCheese() {
        $this->cheese = true;
        return $this;
    }

    public function withPepperoni() {
        $this->pepperoni = true;
        return $this;
    }

    public function withMushrooms() {
        $this->mushrooms = true;
        return $this;
    }

    public function build() {
        // Creates the final Pizza object with all the collected options
        return new Pizza($this->size, $this->cheese, $this->pepperoni, $this->mushrooms);
    }
}

// Create a builder for a "large" pizza
$pizzaBuilder = new PizzaBuilder("large");

// Add toppings and build the pizza
$pizza = $pizzaBuilder
    ->withCheese()
    ->withPepperoni()
    ->withMushrooms()
    ->build();

// Print the pizza
echo $pizza; // Output: Pizza: Size - large, Toppings - cheese, pepperoni, mushrooms
```
[BACK TO THE GUIDE](/en/posts/design-patterns/)

