PHP : OOP - Constructor

In PHP, a constructor is a special method within a class that is automatically called when an object of that class is created. The purpose of a constructor is to initialize the object's properties or perform any setup that is necessary before the object can be used. Constructors are defined using the __construct keyword.

Constructor Syntax:


class ClassName {
    public function __construct() {
        // Constructor code
    }
}

 

Example of a Constructor:

Here's an example to illustrate how constructors work in PHP:


class Person {
    public $name;
    public $age;

    // Constructor
    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
        echo "New person object created.<br>";
    }

    public function displayInfo() {
        echo "Name: {$this->name}, Age: {$this->age}<br>";
    }
}

// Creating an object of the Person class
$person1 = new Person("John", 30);

// Calling a method on the object
$person1->displayInfo(); // Output: Name: John, Age: 30

 

In this example:

  • The Person class has a constructor __construct that takes two parameters: $name and $age.
  •  When a Person object is created ( $person1 = new Person("John", 30) ), the constructor is automatically called.
  • Inside the constructor, the object's properties ( $this->name and $this->age ) are initialized with the provided values.
  • The constructor also outputs "New person object created." when the object is created.
  • After creating the object, we call the displayInfo() method to display the person's name and age.

Constructor Overloading:

PHP does not support constructor overloading in the same way as some other languages. This means you cannot define multiple constructors with different numbers or types of parameters. However, you can achieve similar behavior using default parameter values or by checking the number of arguments passed.

Example with Default Parameter:


class Person {
    public $name;
    public $age;

    // Constructor with default values
    public function __construct($name = "Unknown", $age = 0) {
        $this->name = $name;
        $this->age = $age;
        echo "New person object created.<br>";
    }

    public function displayInfo() {
        echo "Name: {$this->name}, Age: {$this->age}<br>";
    }
}

// Creating objects with different constructor arguments
$person1 = new Person(); // Uses default values
$person2 = new Person("Alice", 25);
$person3 = new Person("Bob");

$person1->displayInfo(); // Output: Name: Unknown, Age: 0
$person2->displayInfo(); // Output: Name: Alice, Age: 25
$person3->displayInfo(); // Output: Name: Bob, Age: 0

 

Constructor Chaining:

Constructor chaining refers to calling one constructor from another within the same class. This can be useful for reducing duplicate code if you have multiple constructors that share some common initialization logic.

Example of Constructor Chaining:


class Person {
    public $name;
    public $age;

    // Constructor with default values
    public function __construct($name = "Unknown", $age = 0) {
        $this->name = $name;
        $this->age = $age;
    }

    // Another constructor that chains to the default constructor
    public static function createFromName($name) {
        return new self($name);
    }

    public function displayInfo() {
        echo "Name: {$this->name}, Age: {$this->age}<br>";
    }
}

// Creating objects using constructor chaining
$person1 = new Person(); // Uses default values
$person2 = Person::createFromName("Alice");

$person1->displayInfo(); // Output: Name: Unknown, Age: 0
$person2->displayInfo(); // Output: Name: Alice, Age: 0 (from default constructor)

 

Conclusion:

Constructors in PHP are special methods that are automatically called when an object is created. They are used to initialize object properties or perform setup tasks. Constructors cannot be directly called like regular methods; they are invoked automatically when an object is created. Constructor overloading is not directly supported in PHP, but you can achieve similar behavior using default parameter values or by chaining constructors. Understanding constructors is fundamental to working with object-oriented programming in PHP.