Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ellajhonm's avatar

What and Why We Should Use PHP Constructors

Please check if my understanding of PHP Constructors is correct. If accurate, this may help fellow PHP learners understand constructors better as well.

I'm reviewing OOP concepts for Laravel and began to understand constructors better by comparing a class with constructors and without. Let’s say we want to make a House class. All houses must have a type and a color before they can be used.

Without Constructors

class House{
        public $type;
        public $color;

        function set_housetype_and_color($housetype, $color)
        {
            $this->type = $housetype;
             $this->color = $color;
        }

        function get_house(){
            $house = "A ".$this->color." ".$this->type;
            return $house;
        }

    }

Main Program:

$myHouse = new House();
$myHouse->set_housetype_and_color("Bungalow","Red")

Notice the length of the code: that extra line we have to write just so we can have a Red Bungalow for our house. When we compare it to real life, we do have a new House(); , but it’s not fully constructed and usable yet; it still doesn't have a specified type and color. You just have a plain brick and wood template of a house. In real life, you still have to do the extra work of painting and building the house to your desired color and type ($myHouse->set_housetype_and_color("Bungalow","Red");). More lines, work, and time to be exerted.

With Constructor

class House{
        public $type;
        public $color;

        function __construct($housetype, $color)
        {
            $this->type = $housetype;
            $this->color = $color;
        }

	function get_house(){
            $house = "A ".$this->color." ".$this->type;
            return $house;
        }

    }

Main Program

$myHouse = new House("Bungalow","Red");

Observe the fewer lines. Here, upon creation of your House (object), you are given a new house that's been all set up with all the attributes you need already incorporated, ready for us.

Feel free to add corrections or more points, Thank you!

0 likes
9 replies
bugsysha's avatar
bugsysha
Best Answer
Level 61

I'm not a fan of your example and explanation. Also, code is not the best.

All houses must have a type and a color before they can be used.

Here I understand what you are saying, but that is not how a business would explain it, and also not the way you would talk about it in real life.

You would probably say something like: "House must be of a specific type that we can build and a specific color that we have in stock".

Notice the length of the code: that extra line we have to write just so we can have a Red Bungalow for our house.

The length of the code doesn't mean anything in programming. We need to capture business behavior with it which sometimes can lead to longer code.

You just have a plain brick and wood template of a house. In real life, you still have to do the extra work of painting and building the house to your desired color and type ($myHouse->set_housetype_and_color("Bungalow","Red");).

You see how you've mentioned some terms here but they were not captured in code. So apparently from what you've said, a house can be built without color, but it would be plain brick and wood. Also, you are mentioning painting and even building and there is no reference to it in the code.

Observe the fewer lines. Here, upon creation of your House (object), you are given a new house that's been all set up with all the attributes you need already incorporated, ready for us.

Again, fewer lines don't mean anything.

So to sum it up. From your description house has a true dependency only on type, bricks, and wood. House can be painted and even built. So you need to capture those things within the code. You went about describing things that are not related to the constructor. The point of the constructor is simple. It provides an initial state and/or nesesities required for building that object which is commonly called dependencies.

1 like
martinbean's avatar

@ellajhonm Constructors are for constructing classes. That’s it. They’re named so because that’s exactly what they do.

So if a class has some information it needs to be created, you can pass that information as arguments to its constructor. Now it should be impossible (theoretically) for the class to be instantiated without having the information it needs.

You could use setter methods, but this doesn’t enforce anything. A person unfamiliar with the class can instantiate the class, forget to/not know to call the setter method, then call a second method that’s now going to throw an error because it didn’t have the required information that would have been set using the setter method. This scenario wouldn’t be possible using a constructor that mandates the required arguments to be supplied in the creation of the class itself.

So that’s all there is to it. If a class needs something to be constructed successfully, pass it as arguments to its constructor.

1 like
jlrdw's avatar

A constructor is mainly used when parameters are needed in the class, example constructor from a paginator in another framework;

    public function __construct($options=[])
    {
        foreach ($options as $key => $value) {
            $this->$key = $value;
        }
         $this->setPage();
    }

It's that simple. If calling a method direct, parameters are then passed to method.

Note in the above options are passed in, however any missing options then the "default" value is used"

    public $perPage = 5;

So if I have 10 passed in the $options array, then 10 is used, else 5 is used.

But really it's fairly obvious what a constructor is for.

1 like

Please or to participate in this conversation.