InterviewSolution
Saved Bookmarks
| 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. ExampleConstructor class Animal { public $name = "No-name animal"; public function __construct() { } } 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;
|
|