Constructors and Destructors

Introduction

Constructors and destructors are special functions in object-oriented programming. They are mainly used in programming languages such as C++ and PHP classes to manage objects automatically.

A constructor is called when an object is created, while a destructor is called when an object is destroyed or removed from memory.

Understanding constructors and destructors helps developers write cleaner, more efficient, and organized programs.

Objectives

By the end of this training, you will be able to:

  • Understand constructors and destructors
  • Learn how constructors initialize objects
  • Understand how destructors release resources
  • Create classes using constructors and destructors
  • Improve object-oriented programming skills

What is a Constructor

A constructor is a special method that automatically runs when an object of a class is created.

Constructors are mainly used to:

  • Initialize object properties
  • Assign default values
  • Execute setup tasks automatically

Features of Constructors

  • Automatically called during object creation
  • Has the same name as the class in some languages
  • Can accept parameters
  • Helps reduce repetitive code

Constructor Example in PHP

<?php
class Student {

public $name;

function __construct($studentName) {
$this->name = $studentName;
echo "Student Name: " . $this->name;
}
}

$obj = new Student("Ali");
?>

Output

Student Name: Ali

Constructor with Multiple Parameters

<?php
class Employee {

public $name;
public $salary;

function __construct($empName, $empSalary) {
$this->name = $empName;
$this->salary = $empSalary;
}

function display() {
echo $this->name;
echo $this->salary;
}
}

$emp = new Employee("Ahmed", 50000);
$emp->display();
?>

Advantages of Constructors

  • Saves development time
  • Automatically initializes data
  • Improves code readability
  • Reduces manual setup work
  • Makes programs more organized

What is a Destructor

A destructor is a special method that automatically runs when an object is destroyed.

Destructors are used to:

  • Close database connections
  • Release memory
  • Remove temporary files
  • Perform cleanup operations

Features of Destructors

  • Automatically called at the end of an object’s life
  • Cannot accept parameters
  • Used for cleanup tasks
  • Helps manage resources efficiently

Destructor Example in PHP

<?php
class Test {

function __construct() {
echo "Object Created";
}

function __destruct() {
echo "Object Destroyed";
}
}

$obj = new Test();
?>

Output

Object Created
Object Destroyed

Real World Example

In web applications:

  • Constructors are used to connect to databases when objects are created
  • Destructors are used to close database connections automatically

This improves performance and resource management.

Difference Between Constructor and Destructor

ConstructorDestructor
Called when object is createdCalled when object is destroyed
Used for initializationUsed for cleanup
Can accept parametersCannot accept parameters
Executes at the startExecutes at the end

Best Practices

  • Use constructors to initialize important data
  • Use destructors only for cleanup tasks
  • Keep constructor logic simple
  • Avoid unnecessary resource usage

Applications of Constructors and Destructors

Constructors and destructors are commonly used in:

  • Web development
  • Database management systems
  • Game development
  • Software applications
  • Enterprise systems

Career Benefits

Learning constructors and destructors helps students become:

  • PHP Developers
  • C++ Programmers
  • Backend Developers
  • Software Engineers
  • Object-Oriented Programming Experts

Final Presentation

In your final presentation, explain:

  • What constructors are
  • What destructors are
  • Differences between constructors and destructors
  • Real-world applications
  • Benefits of using constructors and destructors
  • Example programs using classes and objects
Home » Advanced PHP > Object-Oriented PHP > Constructors and Destructors