Tutorials

PHP Magic Method __construct() tutorial

Posted by I. B. Gd Pramana A. Putra, 13 Mar 22, last updated 18 Jul 22

In this lesson, we’re going to learn and get familiar with PHP __construct() Magic Method tutorial. PHP __construct() is the opposite of PHP __destruct().

PHP Magic Method: __construct Definition

What is PHP __construct() magic method?

__construct() in PHP programming language is one of the magic methods that will firstly be called when an object of a class is instantiated. If a constructor is not declared when the class is being instantiated as an object, the constructor is remaining exists but without any property defined.

PHP Magic Method: __construct Declaration

__construct() magic method in PHP is declared with the following format:

__construct ([ mixed $args = "" [, $... ]] ) : void

The declaration format above tells us that this magic method is accepting many arguments with mixed data types (can be anything), and the type of this method is a void.

PHP Magic Method: __construct Example

__constructor is a magic method that will be called first once an object of a class is instantiated. A constructor is intended to give values to the properties of the class when it created as an object (instantiated). An example that we often see is a class to connect PHP with SQL databases (MySQL / PostgreSQL).

To connect with SQL database, we have to know the source name of the database (dsn), the username to connect to the database, and the password. These are the data attributes that we define inside the __construct, so that when the class is instantiated, the newly created object has already persists with the data that it needs to connect with an SQL database.

DBConnect is a class that we will use in this __constructor tutorial. This class is used to connect PHP with the database, whether it’s MySQL or PostgreSQL.

class DbConnect
{
    protected $connection;
    private $dsn, $username, $password;
    
    public function __construct($dsn, $username, $password)
    {
        $this->dsn = $dsn;
        $this->username = $username;
        $this->password = $password;
        $this->connect();
    }
    
    public function connect()
    {
        $this->connection = new PDO($this->dsn, $this->username, $this->password);
    }
}

When the class is instantiated, the instantiated object will be able to connect with SQL databases.

$db = new DBConnect();

Once the class is instantiated into an object, the constructor method will be directly called/executed, in this example, a new object from DBConnect namely $db will be directly connected to the SQL database.

Another Example

Another example for __construct method implementation is when we create a class to print hello world to user.

class MessageBoard
{
    private $name;
    
    public function __construct($name)
    {
        $this->name = $name;
    }
    
    public function sayHello()
    {
        return "Hello, have a good day " . $this->name;
    }
}

In the class above there’s a private property, which that property will be assigned with a value inside the constructor. The sayHello method that will return a string that prints out hello sentences to the users.

When we create an instance of that class, we need to give an argument:

$messageToPramana = new MessageBoard("Pramana");

When we call the sayHello method after instantiating the object, we will get the output as “Hello, have a good day Pramana”.

$messageToPramana = new MessageBoard("Pramana");
echo $messageToPramana->sayHello();

Output

As a result, we will get to see our PHP application returns us whatever strings that we put on sayHello method.

Reference: Construct – PHP.net

Answer & Responses
    No comments yet

Wanna write a response?

You have to login before write a comment to this post.