1.

What is constructor and destructor?

Answer»

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;

 



Discussion

No Comment Found