Loading…

What is constructor and destructor?

Constructor and Destructor both are special functions which are automatically called when an object is created and destroyed.

Example

                                        

Constructor

class Animal

{

     public $name = "No-name animal";

     public function __construct() {

     echo "I'm alive!";

}

}

Destructor

class Animal {

public $name = "No-name animal";

public function __construct($name) {

echo "I'm alive!";

$this->name = $name;

}

public function __destruct() {

echo "I'm dead now :(";

}

}

$animal = new Animal("Bob");

echo "Name of the animal: " . $animal->name;

 

Suggest An Answer

No suggestions avaliable!

Latest post from oops interview questions

Ask Question