PHP Constructor

Created with Sketch.

PHP Constructor

Summary: in this tutorial, you will learn how to use the PHP constructor to initialize the 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 __construct($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 creating a 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 redundant.

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

For example, instead of writing this:

<?php

class BankAccount
{
private $accountNumber;

private $balance;

function __construct($accountNumber, $balance)
{
$this->accountNumber = $accountNumber;
$this->balance = balance;
}
}

Code language: HTML, XML (xml)

you can use this:

<?php

class BankAccount
{
function __construct( 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’s argument and an object’s 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 __construct(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.

Leave a Reply

Your email address will not be published. Required fields are marked *