premavansmuuf,

Sadly, no explanation whatsoever of why this approach might be better than just building the object yourself. The example provided is fairly trivial and doesn’t seem to justify using a builder.

Also, how would you do validation of properties (or even detect incompatibility of some of them)? Surely, because Product can be created directly, the Product class itself should validate its values. But it also makes sense for the ProductBuilder to have its own validation, right? Would that mean doing the validation twice?

The article tries to sell the pattern as being your best friend, but at the same time doesn’t say how that’s gonna happen.

abhibeckert, (edited )

The main advantage of a builder is you can create an object in multiple steps instead of one step.

During the builder process the object might temporarily be in an invalid state - for example perhaps you can’t easily access the product price. You wouldn’t want price to be an optional value for the actual product class, but it can be optional for the builder (as long as it’s set once build() is called).

abhibeckert,

You’ve missed out on the biggest feature in PHP 8. With named arguments it’s now possible for arguments to be provided in any order. You can now have functions with a large number of optional parameters, without creating a total shit show.

Instead of writing this:

<pre style="background-color:#ffffff;">
<span style="color:#323232;">$product = $productBuilder
</span><span style="color:#323232;">    ->setId(101)
</span><span style="color:#323232;">    ->setName('iPhone 13')
</span><span style="color:#323232;">    ->setPrice(999.99)
</span><span style="color:#323232;">    ->setDescription('New iPhone 13 with A15 Bionic chip')
</span><span style="color:#323232;">    ->setManufacturer('Apple Inc.')
</span><span style="color:#323232;">    ->setInventory(1000)
</span><span style="color:#323232;">    ->setDiscount(10)
</span><span style="color:#323232;">    ->build();
</span>

You can now do this:

<pre style="background-color:#ffffff;">
<span style="color:#323232;">$product = $productBuilder->set(
</span><span style="color:#323232;">    id: 101,
</span><span style="color:#323232;">    name: 'iPhone 13',
</span><span style="color:#323232;">    price: 999.99,
</span><span style="color:#323232;">    description: 'New iPhone 13 with A15 Bionic chip',
</span><span style="color:#323232;">    manufacturer: 'Apple Inc.',
</span><span style="color:#323232;">    inventory: 1000,
</span><span style="color:#323232;">    discount: 10
</span><span style="color:#323232;">)->build();
</span>
rikudou,

Practical example I have written: github.com/Unleash/…/UnleashBuilder.php

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • Durango
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • tacticalgear
  • khanakhh
  • Youngstown
  • mdbf
  • slotface
  • rosin
  • everett
  • ngwrru68w68
  • kavyap
  • InstantRegret
  • JUstTest
  • cubers
  • GTA5RPClips
  • cisconetworking
  • ethstaker
  • osvaldo12
  • modclub
  • normalnudes
  • provamag3
  • tester
  • anitta
  • Leos
  • megavids
  • lostlight
  • All magazines