Constructor and constructor promoted properties in PHP

Timothy Iloba
6 min readFeb 5, 2024

--

Hey everyone, welcome to this blogpost. In this blogpost, we will be discussing about constructors and constructor promoted properties in PHP. I guess that if you are hearing this for the first time, it kind of sounds scary. It did sound scary for me back in the day but the truth is that it is a very simple concept and I will make sure I explain it in a way you will understand. Let’s dive in.

What is a constructor?

Like in every other object-oriented programming language, in PHP, a constructor is a magic method that is used to help us set up our objects. I’m sure you’re wondering, what is a magic method? A magic method is a special kind of method in PHP that we do not call ourselves. Magic methods are called by PHP automatically whenever we carry out a certain action. So, earlier we agreed that a constructor is a magic method and from what we said about magic methods, we now understand that magic methods are called by PHP automatically when we carry out an action. So, you might want to ask, what action do we need to take for our friend the constructor which is a magic method to be called? It’s really straightforward. Our Constructor, which is a magic method, is called automatically by PHP whenever we instantiate our classes to create a new object.

An interesting thing about this our friend, the constructor, is that it is the first magic method that is called whenever we create an object. Let’s take a look at this example.

Let’s use this analogy, in this analogy, we will have a cake class which we will later use to create many delicious cake objects.

<?php

class Cake
{
//This is our constructor method
public function __construct()
{
print "Hey, I am the Constructor and i am a Magic method that is called whenever you create a new object off this class";

}
}

From the code above, you can see our cake class with a constructor method in it. Amazingly, the constructor method is not named constructor. It is named __construct. It is very important to note that. It starts with double underscores and the word construct. Very simple. So, we can see our constructor method and it is just printing a message to us.

Let’s run this code below

<?php

require 'Cake.php';

$cake = new Cake();

If you did everything right, you will get the following results below

Hey, I am the Constructor and i am a Magic method that is called whenever you create a new object off this class

Cool, I am sure you got the same results but don’t celebrate yet, let us understand why we got the results that we had.

From the code execution above, you could see that we just created a new cake object by instantiating the cake class. We only had one method in the class which was the constructor and as you can see, we did not call it. And from our results, we can tell that someone called the constructor method. The question now is, who called our friend the constructor method?? The answer is obvious and I am sure you can remember. The constructor method is a magic method that is called immediately when we create a new object. It’s simple to understand. Whenever we create an object, PHP does the magic. It checks our code, looking for a constructor method and if it finds one, it runs it automatically for us. Very fast and quick like magic. That is why the constructor method is a magic method.

Now that we have understood that the constructor is called whenever we create an object, let us move forward to see why we need this magic method called the constructor. If you can recall from the above definition we gave, a constructor is a magic method that is used to help us set up our objects. It just helps us get our objects ready.

So, from our above example, let’s say we need to create a cake object from the cake class. First of all, let’s examine this. In a real-life scenario, if we are to create an actual cake, we will need to get our ingredients together, put them in a bowl, mix them and then bake it.

Back to our code. In our case here, our constructor is that bowl that will mix our ingredients and get our cake ready to be baked. Remember that our constructor helps us set up our objects. Let’s write some code so you will understand better. In this code example, we will need to give our cake a name and a type. Our constructor will help us do that.

<?php

class Cake
{
//our name and type properties
public string $name;
public string $type;


//our constructor setting up our cake object and giving it a name and type
public function __construct(string $name, string $type)
{
$this->name = $name;
$this->type = $type;
}
}

From the code above, you can see that we have added two properties to our class (name and type) and here is the interesting part. You can see that our constructor is now accepting parameters. It is accepting a name and a type parameter and it is assigning the values to our name and type property, setting up our objects and getting them ready.

Let’s run this code

<?php

require 'Cake.php';

$cake = new Cake('TimothyCake', 'Vanilla');

print $cake->name;
print $cake->type;

From what we can see above, we are creating a new object and this time passing two arguments. Those two arguments are going to be passed to our constructor and our constructor will use them to set up our new cake object for us. It will help give our cake a name and also a type. Our cake will now have a name of TimothyCake and a type of Vanilla. Vanilla flavored cakes taste delicious, I love them, I don’t know about you.

So, now our cake has a name and a type and we can go ahead and print out the name and type as we can see above.

Our results will be

TimothyCakeVanilla

I hope you now understand what the constructor does. It helps us set up our objects. From our example above, it helped give our new cake object a name and a type thereby producing a Vanilla flavored cake named “TimothyCake”.

So that’s it with constructors. Let’s move on to constructor promoted properties.

As usual my friend, don’t get carried away by the way it sounds. Constructor promoted properties is a concept that was introduced in PHP version 8 to help us write less code when using our constructor to set up objects. If you observe the code we used above in our cake class, you will notice that we created two public properties which were the name and type, we then assigned values to those properties in our constructor. That’s cool. But there is a better way of doing it and it’s called constructor promoted properties. Take a look below

<?php

class Cake
{

//our Constructor using promoted properties
public function __construct(
public string $name,
public string $type
) {
}
}

Take a look at our class above. Would you believe me if I told you that it’s a better and quicker version of what we did above in the previous cake class example? Well, it is. Let’s examine the difference between the former example and this.

In this example, you can notice that we no longer have any properties on our class and we are no longer assigning any values in our constructor.

All we simply did was pass the properties directly into our constructor as parameters, hence you can see the visibility keyword “public”, the property type “string: and then the name of the property and that’s it. PHP understands everything and will process it as it did before. So, we can run our code and still get the same results

<?php

require 'Cake.php';

$cake = new Cake('TimothyCake', 'Vanilla');

print $cake->name;
print $cake->type;
TimothyCakeVanilla

So, constructor promoted properties is just a feature that helps us write less code when setting up our objects in our constructor.

If you are interested in seeing Constructors and Constructor Promoted Properties explained in a video format, feel free to check out this full PHP Object oriented course. https://garyclarketech.teachable.com/p/learn-object-oriented-php. Everything you need to understand Object Oriented programming is covered there.

You can grab yours now and get a 20% discount. All you have to do is use this promo code: GCTREPO20 when making your purchase.

You can access all the codes used in this post here: https://github.com/GaryClarke/blog-code-examples/tree/2-constructors-and-constructor-promoted-properties

--

--

Timothy Iloba

Let's work our way up into being world class PHP developers. We will focus on understanding the basics and important concepts as that is what will set us apart.