What is constructor and destructor?
Constructor and Destructor both are special functions which are automatically called when an object is created and destroyed.
BY Best Interview Question ON 13 Jan 2019
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;