PHP Constructor

Summary: in this tutorial, you will learn how to use the PHP constructor to initialize properties of an object.

Introduction to the PHP Constructor

PHP allows you to declare a constructor method for a class with the name __construct() as follows:

<?php class ClassName { function __construct() { // implementation } }
Code language: HTML, XML (xml)

When you create an instance of the class, PHP automatically calls the constructor method:

$object = new ClassName()
Code language: PHP (php)

Typically, you use the constructor to initialize the properties of the object.

The following example defines a constructor for the BankAccount class. The constructor initializes the $accountNumber and $balance properties:

<?php class BankAccount { private $accountNumber; private $balance; public function constructor($accountNumber, $balance) { $this->accountNumber = $accountNumber; $this->balance = balance; } }
Code language: HTML, XML (xml)

Since the constructor has two parameters, you need to pass the corresponding arguments when you create a new BankAccount object:

$account = new BankAccount(1, 100);
Code language: PHP (php)

In this example, PHP automatically calls the __construct() of the BankAccount class. Hence, it assigns the $accountNumber and $balance arguments to the $accountNumber and $balance properties of the object.

PHP Constructor promotion

In practice, you often need to assign the constructor arguments to corresponding properties. It’s kind of redudant.

To improve this, PHP 8.0 introduced the new concept called constructor promotion that promotes the arguments of the constructor to properties.

For example, insteading of writing this:

<?php class BankAccount { private $accountNumber; private $balance; function constructor($accountNumber, $balance) { $this->accountNumber = $accountNumber; $this->balance = balance; } }
Code language: HTML, XML (xml)

You can use this instead:

<?php class BankAccount { function constructor( private $accountNumber, private $balance ) { } }
Code language: HTML, XML (xml)

When a constructor parameter includes an access modifier (public, private, or protected), PHP will treat it as both a constructor argument and an object property. And it assigns the constructor argument to the property.

Sometimes, you don’t want to promote constructor arguments, you can remove the access modifier. For example:

<?php class BankAccount { function constructor ( private $accountNumber, private $balance, $type ) { } }
Code language: HTML, XML (xml)

In this example, the $type argument doesn’t include the access modifier. Therefore, it’s a regular parameter and won’t be promoted to a property.

Notice that the order of promoted-argument and non-promoted arguments can appear in the constructor in any order.

Summary

  • PHP constructor is a special method that is called automatically when an object is created.
  • Do use constructor promotion as much as possible to make the code shorter.
Did you find this tutorial useful?