Interview questions on constructor and destructor in php

1. What is Constructors in php?

A constructor is a method defined inside a class which is called automatically when object is created. A constructor is used to initialize the object.

You can define constructor by using __construct function.

Syntax :

function __construct (mixed $args){ }

2. What is destructor in php?

A destructor is called when the object is destructed or release any object from its memory.

A destructor is used to clean up resources from the memory.

The automatic destruction of class objects is handled by PHP Garbage Collector.

Destructors are for destroying objects and automatically called at the end of execution.

You can define destructor by using __destruct function.

Syntax :
function __destruct() { }

3. What is types of Constructor?

    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 variables.
    Copy Constructor: It accepts the address of the other objects as a parameter.

4. What is Copy Constructor in php?

    Copy Constructor is a type of constructor which is used to create a copy of an already existing object of a class.

    Copy Constructor will pass the address of the other objects as a parameter.

    We can implement copy constructor using __clone() method in php.

5. Write down program for Copy Constructor in php.

<?php
class Registration{
public $name;

public function __construct($name){ // parameterized constructor with name argument
$this->name = $name;
echo "<br>Inside parameterized constructor<br>";
}
public function __clone(){
echo "<br>Inside copy constructor<br>";
}
}
$obj1 = new Registration("john roy"); // parameterized constructor with name argument
$obj2= clone $obj1; // copy constructor initialize $obj2 with the values of $obj1
echo "<br/>Value from parameterized constructor : ".$obj1->name;
echo "<br/>Value from copy constructor : ".$obj2->name;
?>

Output :

Inside parameterized constructor
Inside copy constructor
Value from parameterized constructor : john roy
Value from copy constructor : john roy

6. What is difference between __constructors and __destructors in php?

Comparison between __constructors and __destructors
Constructors Destructors
Constructor is defined by __construct() method Destructors is defined by __destructors method
Constructor accepts one or more arguments Destructors doesn't pass any arguments. It is void.
Constructor is called automatically when the object is created. Destructors is automatically when the object is destroyed.
Constructor has same name as the class. Destructors has same name as the class with prefix ~ (tilda).
Constructor is used to initialize object of a class. Destructors is used to de-initialize objects to free up memory.
Constructor allocates memory for object. Destructors deallocates memory by destroying object.
Constructor can be overloaded. Destructors cannot be overloaded.
Multiple constructors can exist in a class. Only one Destructor can exist in a class.

 

7. What is Advantages of destructors?

=> Destructors is used to destroy objects to free up memory allocation, So that enough space is available for new objects or free up resources for other tasks

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

28552