Introduction
In Object-Oriented Programming (OOP), a constructor is a special type of method that is automatically called when an object of a class is created. In PHP, constructors are commonly used to initialize object properties or perform setup tasks when the object is created.
A constructor helps reduce repetitive code because it allows you to automatically assign values when an object is created.
In PHP, the constructor method is defined using the special method name
__construct(). The name starts with two underscores (__).
Key points about PHP constructors:
- A constructor is a special method used to initialize an object.
- It is automatically executed when an object is created.
- The constructor method is named __construct().
- It is usually declared as public, even if the visibility is not specified
- It does not return any value.
Constructors are very useful when working with classes that require initial values, such as user information, database connections, configuration settings, or object initialization.
Syntax
The basic syntax of a PHP constructor is shown below.
class ClassName {
public function __construct() {
// initialization code
}
}
Example of creating an object:
$object = new ClassName();
Explanation:
- class ClassName → defines the class.
- __construct() → constructor method.
- new keyword → used to create an object.
- When the object is created, the constructor runs automatically.
Example:
1. Create a Default Constructor Using __construct()
Below is a simple example of a default constructor.
class Student {
function __construct() {
echo "There are 90 students in the class";
}
}
$stu = new Student();
Output:
When the object $stu is created, the constructor method runs automatically and prints the message.
Older versions of PHP also allowed the constructor to have the same name as the class, but the recommended way is to use __construct().
2. Create a Default Constructor Using Class Name:
class Student {
function Student() {
echo "There are 90 students in the class";
}
}
$stu = new Student();
Output:
Both examples produce the same result.
Real-world Example
1. Example Of Parameterized Constructor
Let’s look at a real-world example using a parameterized constructor.
Suppose we want to store information about a student, such as name and age.
class Student {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function introduce() {
echo "I'm {$this->name} and I'm {$this->age} years old";
}
}
$stu = new Student("John", 27);
$stu->introduce();
Output:
In this example:
- constructor receives name and age as parameters.
- These values are assigned to object properties.
- The introduce() method displays the student information.
This is commonly used in real-world applications such as user registration systems, product details, or employee records.
2. Example Of Parent Constructor
When a class inherits another class, the parent constructor is not automatically called if the child class defines its own constructor.
To call the parent constructor, we use:
Syntax:
parent::__construct();
Example:
class MainClass {
function __construct() {
echo "In MainClass constructor!";
}
}
class ChildClass extends MainClass {
function __construct() {
parent::__construct();
echo "In ChildClass constructor!";
}
}
$cc_obj = new ChildClass();
Output:
In ChildClass constructor!
Here:
- The child constructor calls the parent constructor explicitly.
- Both constructors execute.
This concept is widely used in inheritance-based applications.
PHP Constructor – Interview Questions
Q 1: What is a constructor?
Q 2: Constructor name in PHP?
Q 3: Can constructor accept parameters?
Q 4: Can a class have multiple constructors?
Q 5: Why use constructors?
Conclusion
A PHP constructor is a powerful feature in Object-Oriented Programming that automatically runs when an object is created. It is mainly used to initialize properties and set up the initial state of an object.
Constructors help reduce repetitive code and ensure that objects start with the required data. PHP supports both default constructors and parameterized constructors, allowing developers to create flexible and reusable classes.
In inheritance scenarios, child classes can also call parent constructors using parent::__construct()
Understanding constructors is essential for mastering PHP OOP because they are widely used in real-world applications such as user management systems, database connections, configuration setups, and framework development.
PHP Constructor – Objective Questions (MCQs)
Q1. Constructor in PHP is a method used to ______.
Q2. Constructor method name in PHP is ______.
Q3. Constructor is called ______.
Q4. Can constructors accept parameters in PHP?
Q5. Which is correct syntax to declare a constructor?