How to Create a Class Constructor in PHP


In this article, we go over how to create a class constructor in PHP.

A constructor is necessary when you want to initialize values for an object when the object is created.

If you are creating an object has no default values or properties assigned to it, then you do not need to create a constructor. A default constructor is already created and called when you instantiate an object that takes in no values.

However, if when you instantiate an object, you want to pass values to that object, you have to create a constructor that specifies what values the object will take in with the constructor.

A constructor is a method that is called when you instantiate (create) an object.

If a constructor has values in the parameters, then these are the values you need to pass to the object at the time of instantiation.

So, say if we create a class named vehicle and we want to create objects of vehicles such as a car, truck, van, jeep, etc.

When we create an object, we want to absolutely know the type and year of the car. For example, a toyota camry, year 2015.

In a case like this, we would create a constructor that has 2 properties, $type representing the type of car and $year representing the year of the car.

The general format for creating a constructor in PHP is shown below.

So the code above creates a constructor in a class that has 2 variables or properties, $type and $year.

To create a constructor, we use public function or private function or protected function based on the type you want followed by 2 underscore symbols and construct (__construct). We then have the variables that we want to pass to an object when it is instantiated. Remember, the constructor is the method that is called when you instantiate an object. Based on this constructor, it determines what values and how many values you pass to the object at the point of instantiation.

So when we create an object based on this constructor, we must pass the type and year of the vehicle.

$this is a keyword in PHP that refers to the object being instantiated. Being that you wouldn't know the name of the object until you create it, $this refers to the name of the object you create when you create it. So since $this refers to the object, you can t think of it like replacing $this with the object name that you create. You then can see more clearly that we are assigned the property type and year to the object we created and assign them the values passed into the object when instantiated.

So when we are creating a constructor in PHP, we always use the keyword $this to refer to the object we instantiate. At the time of instantiation, the $this keyword is replaced by the object name. And, thus, the object gets the values of type and price assigned to it. The values are set equal to the parameters we pass into the object when instantiated.

The full PHP code to create a constructor of a class named vehicles is shown below.

So in the above code, we have created a class named vehicles.

We then create a public constructor with the parameters of $type and $year.

This means that if we create an object in this class, we must pass along 2 parameters, the type and year of the car, or else the program will throw us warnings.

Since we do not know the name of an object until we instantiate one, the keyword $this will refer to the name of the object when it is instantiated. Remember, this constructor method is called when we instantiate an object in this class. At this point, the $this keyword is replaced by the object name. Thus, the object gets assigned the property value type and year when created. And these values are based on what we pass into the parameters of the object when instantiated.

Then below we actually create an object, $car, that has a type value of "Honda Accord" and the year 2009.

The constructor is called. These values get passed into the $car object.

We then echo out the values of the $car object based on the standard way of echoing out object properties. Taking the object name and property with "->" in between.

And this is call that is required to create class constructors in PHP.

Going back to the PHP code above, if it's run, this gives the following output shown below.

Actual PHP Output

Honda Accord 2009

HTML Comment Box is loading comments...