What is Constructor and Destructor

 Constructor is a special type of function which will be called automatically whenever there is any object created from a class.

Destructor is a special type of function which will be called automatically whenever any object is deleted or goes out of scope.

Syntax: 

__construct(): 


function __construct() {

       // initialize the object and its properties by assigning 

       //values

       }


__destruct(): 

function __destruct()  {

       // destroying the object or clean up resources here 

       }

Note: The constructor is defined in the public section of the Class. Even the values to properties of the class are set by Constructors.

Constructor types

Default Constructor:It has no parameters, but the values to the default constructor can be passed dynamically.

Parameterized Constructor: It takes the parameters, and also you can pass different values to the data members.

Copy Constructor: It accepts the address of the other objects as a parameter.

Example

<?PHP

class Tree

{

function Tree()

{

echo "Its a User-defined Constructor of the class Tree";

}


function __construct()

{

echo "Its a Pre-defined Constructor of the class Tree";

}

}


$obj= new Tree();

?>

Output: 

Its a Pre-defined Constructor of the class Tree

Parameterized Constructor: The constructor of the class accepts arguments or parameters. 

The -> operator is used to set value for the variables. In the constructor method, you can assign values to the variables during object creation.

Example -

<?php


class Employee {

Public $name;

Public $position;

function __construct($name,$position) {

// This is initializing the class properties

$this->name=$name;

$this->profile=$position;

}

function show_details() {

echo $this->name." : ";

echo "Your position is ".$this->profile."\n";

}

}

$employee_obj= new Employee("Rakesh","developer");

$employee_obj->show_details();

$employee2= new Employee("Vikas","Manager");

$employee2->show_details();

?>

Output -

Rakesh : Your position is developer

Vikas : Your position is Manager


Constructors start with two underscores and generally look like normal PHP functions. Sometimes these constructors are called as magic functions starting with two underscores and with some extra functionality than normal methods. After creating an object of some class that includes constructor, the content of constructor will be automatically executed.

Note: If the PHP Class has a constructor, then at the time of object creation, the constructor of the class is called. The constructors have no Return Type, so they do not return anything not even void.

Advantages of using Constructors

Constructors provides the ability to pass parameters which are helpful in automatic initialization of the member variables during creation time .
The Constructors can have as many parameters as required and they can be defined with the default arguments.
They encourage re-usability avoiding re-initializing whenever instance of the class is created .
You can start session in constructor method so that you don’t have to start in all the functions everytime.
They can call class member methods and functions.
They can call other Constructors even from Parent class.

Destructor: Destructor is also a special member function which is exactly the reverse of constructor method and is called when an instance of the class is deleted from the memory. Destructors (__destruct ( void): void) are methods which are called when there is no reference to any object of the class or goes out of scope or about to release explicitly. 
They don’t have any types or return value. It is just called before de-allocating memory for an object or during the finish of execution of PHP scripts or as soon as the execution control leaves the block. 
Global objects are destroyed when the full script or code terminates. Cleaning up of resources before memory release or closing of files takes place in the destructor method, whenever they are no longer needed in the code. The automatic destruction of class objects is handled by PHP Garbage Collector.

Note: The destructor method is called when the PHP code is executed completely by its last line by using PHP exit() or die() functions.

Example - 

<?php

class SomeClass {
 
function __construct()
{
echo "In constructor, ";
$this->name = "Class object! ";
}

function __destruct() {
echo "destroying " . $this->name . "\n";
}
}
$obj = new Someclass();

?>

Output -

In constructor, destroying Class object! 

Note: In the case of inheritance, and if both the child and parent Class have destructors then, the destructor of the derived class is called first, and then the destructor of the parent class. 

Advantages of destructors
 
Destructors give chance to objects to free up memory allocation , so that enough space is available for new objects or free up resources for other tasks.
It effectively makes programs run more efficiently and are very useful as they carry out clean up tasks.
 

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...