Using __construct() and __destruct()

Using __construct() and __destruct()

In PHP programming, constructors and destructors are special, unique class methods or functions for user-defined class data types in the object-oriented programming concept. They are automatically called when a class object is created or destroyed by the programmer. In PHP programming, the __construct() and __destruct() class methods are used to create new objects and destroy old objects.

Using __construct() and __destruct()

So, let’s get to know the __construct() and __destruct() class methods in PHP programming better.

__construct() – Constructor in PHP Programming.

What is a Constructor in PHP Programming?

The __construct() class method in PHP programming is a special, unique class object construction method in the PHP language. It is automatically called when a new instantiation of a user-defined class object is created. The constructor method in a class is typically used to initialize object properties or perform setup tasks when a new class object is created.

Syntax of the constructor method.

public function __construct([parameters])

The constructor method in a PHP class program can accept argument parameters that can be passed when creating a new object in an existing class. This allows programmers to initialize the properties of a class with specific values ​​when constructing a new object.

Example of the __construct() method in PHP programming.

<?php

class employee {

public $emp_name;

public $emp_id;

public $emp_dept;

// here we use Constructor method for employee class

public function __construct($emp_name, $emp_id, $emp_dept) {

$this->emp_name = $emp_name;

$this->emp_id = $emp_id;

$this->emp_dept = $emp_dept;

}

public function employeeInfo() {

echo “Employee Name – $this->emp_name, \n Employee Id – $this->emp_id , \n Employee Department – $this->emp_dept \n”;

  }

}

// here we Create an object of the employee class

$employee1 = new employee(“Siddhi Deora”, “S101”, “Marketing”);

$employee1->employeeInfo(); // Result – Employee Name – Siddhi Deora, Employee Id – S101, Employee Department – Marketing

?>

__construct() method in PHP Explanation.

  • In this program, the __construct() class method is called when creating the $employeer1 class instance.
  • The “Siddhi Deora”, “S101”, and “Marketing” class parameters are passed to the constructor, which initializes the employee class object’s properties $emp_name, $emp_id, and $emp_dept.

__destruct() – Destructor in PHP Programming.

What is a destructor in PHP Programming?

The __destruct() class method in PHP is another special and unique class object destruction method in PHP. It is automatically called when a user-defined class object is destroyed and the object goes out of scope. The __destruct() class method is used to perform any class program cleanup task, such as stopping a database connection, releasing class object resources, or applying finalization operations before removing the class object from storage memory.

Syntax of __destruct() in PHP Programming.

Public function __destruct()

Here, the destructor function in a class program does not accept any parameters.

Remember, the __destruct() method in a class program is automatically invoked when an object is destroyed, the script terminates, or in some particular condition where the class object is still in scope.

Example of __destruct() in PHP Programming.

<?php

class employee {

public $emp_name;

public $emp_id;

public $emp_dept;

// here we use employee class Constructor method

public function __construct($emp_name, $emp_id, $emp_dept) {

$this->emp_name = $emp_name;

$this->emp_id = $emp_id;

$this->emp_dept = $emp_dept;

echo “Employee object created \n Employee Name – $this->emp_name, \n Employee Id – $this->emp_id , \n Employee Department – $this->emp_dept \n \n”;

   }

//here employee class Destructor method use

public function __destruct() {

echo “Employee object destroyed – $this->emp_name $this->emp_id $this->emp_dept \n”;

    }

}

//here we Create an object of the employee class

$employee1 = new employee(“Siddhi Deora”, “S101”, “Marketing”);

echo “employee Object is still in use\n”;

// here this code use to Destroy the object explicitly (optional)

unset($employee1); // here The destructor method will be called when the object is destroyed

echo “Terminate the process.\n”;

?>

Explanation of the __destruct() class method.

  • In this program, the __construct() class method is called when the $employee1 object is constructed, and it prints a message to the console screen.
  • Here, the __destruct() class method is called when the class object is destroyed using the unset() function. If the created class object is not destroyed explicitly in this program, PHP will automatically call the destructor in sequence when the process terminates and the class object goes out of scope.

Key points about __construct() and __destruct() in PHP programming.

__construct() class method.

  • When a new class object is created in a user-defined class, it is automatically invoked in sequence.
  • The __construct() method in a class is used to initialize the class object’s properties or complete setup tasks.
  • The __construct() method can accept parameter values ​​to initialize a class object with specific values.

__destruct() class method.

  • When an object in a user-defined class is destroyed, or the current class goes out of scope, the __destruct() method is automatically invoked in order.
  • In PHP class programs, the __destruct() function method is used for system cleanup tasks, such as terminating a database connection, releasing program resources, or performing the final program operation.
  • When a script in a program terminates, and no more references to the class object exist, PHP will automatically call the destructor method in order.

Order of execution of the __construct() and __destruct() functions.

  • In a class program, the __construct() method is called first, immediately after a class object is instantiated, created, or defined.
  • The __destruct() method in a class program is called at the very end of the program, when an object in the current class program is destroyed, or the class program script terminates.

Memory Management in PHP Class Programs.

PHP programming uses garbage collection for memory management. When there are no more references to an object in a class program, it is eventually destroyed in the program, so the __destruct() class method will be activated at the end of the program.

Example of how both a constructor and a destructor work in PHP.

<?php

class developer {

public $developer_name;

public $dev_id;

public $email;

// Here we use the Constructor method for the developer class

public function __construct($developer_name, $dev_id, $email) {

$this->developer_name = $developer_name;

$this->email = $email;

$this->dev_id = $dev_id;

echo “developer object construct – $this->developer_name $this->dev_id [$this->email] \n \n”;

}

// here we use Destructor method for class

public function __destruct() {

echo “developer object destruct – $this->developer_name $this->dev_id [$this->email] \n”;

}

}

// here we Create developer class object

$developer1 = new developer(“Harry Deora”, “H101”, “harry@domain.com” );

echo “developer class object still in use.\n\n”;

// here The object is destroyed when the script ends or unset is called in program

unset($developer1); // here the destructor will be called explicitly here

echo “Terminate of process\n”;

?>

Explanation of the constructor and a destructor method.

  • In this program, when $developer1 is created, the constructor is invoked and prints the constructor message.
  • When unset($developer1) is called in the program, the destructor method is invoked and prints the destructor message.
  • If programmers do not use the unset() function here, the destructor will be called even when the program process terminates execution and the object goes out of scope.

Destructor method without unset() in PHP programming.

If programmers do not call the explicit unset() function in a class program, PHP will automatically destroy the object in order when the program script or function goes out of scope.

Destructor method without unset() in PHP example.

<?php

class Filecontroller {

public $name_of_file;

//here we use filecontroller Constructor method

public function __construct($name_of_file) {

$this->name_of_file = $name_of_file;

echo “Open the file – $this->name_of_file\n”;

}

// here we use filecontroller Destructor method

public function __destruct() {

echo “Close the file – $this->name_of_file \n”;

   }

}

// Create Filecontroller object

$file = new Filecontroller(“file.txt”);

echo “Work with file\n”;

// here filecontroller class Object goes out of scope here, so now destructor will be called automatically

?>

In this program example, the class object goes out of scope at the end of the script, and the destructor is automatically called to follow the file closing process.

A summary of the __construct() and __destruct() method functions in PHP programming.

  • The __construct() function method in PHP programs is used to initialize a class object upon creation, and it can accept class parameters.
  • The __destruct() function method in PHP programs is automatically called when a class object is destroyed or the program goes out of scope, and is useful for cleanup tasks.
  • Both the __construct() and __destruct() methods in PHP programs are essential for program resource management, allowing programmers to properly initialize and clean up the resources needed by class program objects.

Leave a Reply