In this article, we will discuss the basics of builders in PHP. You will learn the purpose of PHP builders and how to use them.

What is a builder?

Here is the official definition:

Builders are conventional methods called during the instantization of a similar object. As such, they can define an arbitrary number of arguments that may be needed, which can have a type and a default value. Builder arguments are invoked by placing the arguments in parentheses after the class name. The PHP Handbook: Builders and Destroyers

When you instantiate an object of any class and if that class has specified a constructor method, it is called automatically. If mandatory constructs are specified in the constructor method, you must also pass these arguments when you instantiate an object of that class.

Let’s quickly look at what the constructor method looks like in PHP.

So you can specify a builder class. You must specify __construct method in the class, and it will become the constructor method for your class. In the example above, whenever you do right away tutsplus object, __construct the method is called first. It is important to note that the constructor method must be defined as public, you cannot instance a class object!

Before PHP 5, to define a constructor, you use a function with the same name as the class in which it is defined, and it is treated as a constructor method! You may be familiar with this way of reporting builders of other programming languages, such as C ++. In PHP, they are called PHP4-style constructors, and it is not recommended to use this style because they will be removed sooner or later.

Let’s check the previous example to see what the builder would look like in the previous version of PHP:

At the time of writing, this example was still working. Give it a try!

What is the purpose of the builder?

Now you know how to declare a constructor method in PHP, but what are the benefits of a constructor method? We discuss it in this section with a few real-life examples.

Initializing the required features

The main purpose of the constructor method is to initialize the properties of an object when it is created. The advantage of this approach is that you do not have to call setter methods later to start the properties of the object after the object is created. So if you think an object needs to have a few properties, the constructor method is the right way to go!

Let’s quickly go through the following example to understand how it works.

As you can see when tutsplusAuthor the object is instanted, __construct the method is called. __construct method expects two arguments, and we passed these two arguments when creating a new object: new tutsplusAuthor("John", ""). Finally, we have initialized author_name and author_email properties for the corresponding values ​​to be transmitted __construct method. So this way you can initialize object properties with the constructor method.

If you tried to instant tutsplusAuthor object without passing arguments, you may make a fatal error, as shown in the following section.

So you need to make sure you pass the required arguments when you speed up tutsplusAuthor object.

Type Check Builder Arguments

In fact, you can also do a type check if you want arguments to be of a certain type.

In the example above, you must enter the first two arguments of the type string and the last argument must be of type array. If you passed the string value in the last argument, you get the following error:

So this is how you can do an exact type check with the builder’s arguments.

Initiating database connection

Another purpose of the builder is to perform some I / O functions: for example, open a file and start reading its contents, initiate an API request, or establish a database connection.

Let’s look at an example of a constructor that creates a database connection.

How to call a parent builder?

In this last section, we’ll see how you can call older class builders. If you know the concept of inheritance, you know that a class can extend another class based on its functions.

It is now possible that both the child and parent classes have their own builders. So how would you call a parent class builder when a child class object is instantiated?

Let’s try to understand it with an example.

As you see, Employee class expands Person class. Now that Employee the object is instanted, __construct() method Employee the class is called. In this method we have used a special parent keyword call __construct() parent class method. And so it ended up playing __construct() method Person class. There is always a good way to call __construct() parent class method if your class expands the class.


Today we discussed builders in PHP along with a few real life examples. You saw how to create a builder, and some use cases for builders in PHP classes.

Check out some of our other posts on PHP object-oriented programming!


Please enter your comment!
Please enter your name here