In this tutorial, we’ll explore the PHP Constructors, a special function within a class. Its primary purpose is to initialize the properties of each object instance. The constructor is remarkable because it’s automatically invoked when a new object is created. Let’s delve into the details of this essential feature.
Page Contents
Please Note, that the construct function starts with two underscores (__).
Here you can check an example program to understand how to create a constructor in PHP:
<?php
class Example{
//defining constructor
function __construct() {
echo "This is Constructor in Example Class";
}
}
$obj= new Example();
?>
Types of Constructor:
- Default Constructor
- Parameterized Constructor
- Copy Constructor
Default Constructor:
A constructor with no parameters is referred to as the default constructor. While it can be used to initialize class properties, it also allows for calling other member functions within the class. This flexibility enhances the default constructor’s utility in managing class behaviour.
<?php
class Example{
//defining constructor
function __construct() {
$this->sayHello();
echo "This is Constructor in Example Class";
}
function sayHello() {
echo "Hello<br>";
}
}
$obj= new Example();
?>
Parameterized Constructor:
A constructor with parameters is called a parameterized constructor. This allows us to provide different values to initialize the data members of the class. The example program below illustrates how to use a Parameterized PHP Constructor, making it easier to grasp the concept.
<?php
class Example{
//defining constructor
function __construct($name) {
echo "Hello ".$name."<br>";
}
}
$obj1= new Example("World");
$obj2= new Example("Owlbuddy");
?>
Copy Constructor:
The copy constructor is used to create a duplicate of an already existing object. It accepts the address of another object as a parameter, allowing for the creation of an identical copy.
<?php
class Example {
public $name;
public $age;
//default constructor
public function __construct() {
}
// This is a copy constructor
public function copyCon(Example $o)
{
$this->name = $o->name;
$this->age = $o->age;
}
function show() {
echo "Name = ".$this->name.'<br>';
echo "Age = ".$this->age.'<br>';
}
}
$obj1 = new Example();
$obj1->name = 'Aman';
$obj1->age = '21';
$obj1->show();
$obj2 = new Example();
//calling copyCon method
$obj2->copyCon($obj1);
$obj2->show();
?>
Parvesh Sandila is a results-driven tech professional with 8+ years of experience in web and mobile development, leadership, and emerging technologies.
After completing his Master’s in Computer Applications (MCA), he began his journey as a programming mentor, guiding 100+ students and helping them build strong foundations in coding. In 2019, he founded Owlbuddy.com, a platform dedicated to providing free, high-quality programming tutorials for aspiring developers.
He then transitioned into a full-time programmer, where his hands-on expertise and problem-solving skills led him to grow into a Team Lead and Technical Project Manager, successfully delivering scalable web and mobile solutions. Today, he works with advanced technologies such as AI systems, RAG architectures, and modern digital solutions, while also collaborating through a strategic partnership with Technobae (UK) to build next-generation products.
