Understanding PHP Constructors

In this article, we’re going to discuss the basics of constructors in PHP. You’ll learn the purpose of PHP constructors and how to use them.

What Is a Constructor?

Here’s the official definition:

Constructors are ordinary methods which are called during the instantiation of their corresponding object. As such, they may define an arbitrary number of arguments, which may be required, may have a type, and may have a default value. Constructor arguments are called by placing the arguments in parentheses after the class name. PHP Manual: Constructors and Destructors

When you instantiate an object of any class, and if that class has defined the constructor method, it would be automatically called. If the constructor method has defined any mandatory arguments, you must pass those arguments as well when you instantiate an object of that class.

Let’s quickly see how a constructor method looks like in PHP.

So that’s how you can define a constructor in your class. You must define the __construct method in your class, and it becomes the constructor method for your class. In the above example, whenever you instantiate the tutsplus object, the __construct method will be called first. It’s important to note that you must define a constructor method as public, you won’t be able to instantiate an object of your class!

Prior to PHP 5, to define a constructor, you would use a function with the same name as the class it is defined in, and it would be treated as a constructor method! You might be familiar with this way of declaring constructors from other programming languages like C++. In PHP, they are called PHP4-style constructors, and it’s not recommended to use this style, since they are going to be deprecated sooner or later.

Let’s revise the previous example to see how the constructor would look in a previous version of PHP:

At the time of writing, this example still worked. Try it out! 

What’s the Purpose of a Constructor?

Now you know how to declare a constructor method in PHP, but what is the use of a constructor method? That’s what we’ll discuss in this section with a couple of real-world examples.

Initializing Required Properties

The main purpose of a constructor method is to initialize an object’s properties when it’s being created. The benefit of this approach is that you don’t have to call setter methods later on to initialize the object’s properties after an object is created. So if you think that an object must have a few properties set, a constructor method is the way to go!

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

As you can see, when the tutsplusAuthor object is instantiated, the __construct method is called. The __construct method expects two arguments, and we’ve passed those two arguments when we create the new object: new tutsplusAuthor("John", "[email protected]"). Finally, we’ve initialized the author_name and author_email properties to the corresponding values that are passed in the __construct method. So in this way, you can initialize object properties with a constructor method.

If you tried to instantiate the tutsplusAuthor object without passing any arguments, you would get a fatal error as shown in the following snippet.

So you need to make sure that you pass any required arguments when instantiate the tutsplusAuthor object.

Type-Checking Constructor Arguments

In fact, you can also do type checking as well if you want that the arguments must be of specific types.

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

So that’s how you can do strict type checking with constructor arguments.

Initializing a Database Connection

Another use of a constructor is to do some I/O: for example, opening a file and starting to read its contents, beginning an API request, or creating a database connection.

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

How to Call a Constructor of the Parent Class?

In this last section, we’ll see how you can call parent class constructors. If you are aware of the concept of inheritance, you would know that a class can extend another class to build on its functionalities.

Now, there’s a possibility that both child and parent classes have their own constructors. So how would you call the constructor of the parent class when a child class object is being instantiated?

Let’s try to understand it with an example.

As you can see, the Employee class extends the Person class. Now, when the Employee object is instantiated, the __construct() method of the Employee class is called. In this method, we’ve used the special parent keyword to call the __construct() method of the parent class. And thus, it would end up calling the __construct() method of the Person class. It’s always a good practice to call the __construct() method of the parent class if your class extends any class.

Conclusion

Today, we discussed constructors in PHP along with a couple of real-world examples. You saw how to create a constructor, and some use-cases for constructors in PHP classes.

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

Leave a comment

Your email address will not be published.