What are constructor and destructor in PHP?

Answer

PHP constructor and  destructor are special type functions which are automatically called when a PHP class object is created and destroyed. The constructor is the most useful of the two because it allows you to send parameters along when creating a new object, which can then be used to initialize variables on the object.

 

<?php class Foo
{ private $name; private $link; public function __construct($name) { $this->;name = $name;
}
 
public function setLink(Foo $link){
$this->;link = $link;
}
 
public function __destruct() {
echo 'Destroying: ', $this->name, PHP_EOL;
}
}
?>

All php Questions

Ask your interview questions on php

Write Your comment or Questions if you want the answers on php from php Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---