Courses Job Ready Fresher Training AI for Class 7 to 12 Corporate Training Placements Tutorials
Free Learning Resources

IT Tutorials & Interview Prep

Free guides, interview Q&As, and job responsibility breakdowns — curated by industry veterans to help you crack MNC interviews

109+
Tutorial Articles
9
Topic Categories
100%
Free to Read
← Back to PHP

Constructors and Destructors

PHP Last Updated: Oct 17, 2025

Constructors and Destructors

Constructor

In PHP, a constructor allows you to initialize an object's properties upon the creation of the object. The constructor function starts with two underscores (__) and if we create a __construct() function, then PHP will automatically call this function when we create an object from a class.

Example:

<?php
class Employee {
 public $name;
 public $surname;

 function __construct($name) {
   $this->name = $name;
 }
 function get_name() {
   return $this->name;
 }
}
$emp1 = new Employee("Lovish");
echo $emp1->get_name();
?>

Output:

Lovish

Destructor

Destructor is a special member function which is exactly the reverse of the constructor method. When it is called, an instance of the class is deleted from the memory. The Destructor function starts with two underscores (__) and if we create a __destruct() function, then PHP will automatically call this function at the end of the script.

Example:

<?php
class Employee {
 public $name;

 function __construct($name) {
   $this->name = $name;
 }
 function __destruct() {
   echo "Employee name is {$this->name}.";
 }
}

$emp1 = new Employee("Izhaan");
?>

Output:

Employee name is Izhaan.

Want to practice these scenarios in a real IT environment? Our 45-Day Internship Program gives you hands-on experience with Active Directory, Ticketing Tools, and Networking.
Join the Internship →