Sometimes if you have a complex class, which needs to be set up in a specific way: Perhaps it takes a lot of constructor arguments, or often requires calling some setters.
Having something like that can make it more difficult to understand the code. In a case like this, you may want to consider using a builder class, which is kind of like a factory, but focused on just creating a single object. It can also be used to hide which concrete class the code is actually using by abstracting the construction.
Let’s take a look at the builder pattern in PHP. We’ll even get to use some chaining – everyone loves chaining, right?
Read More