adplus-dvertising
PHP Interview Questions
What are constructor and destructor in PHP ?
PHP constructor and destructor are special type functions that are automatically called when a PHP class object is created and destroyed.
 
Generally, Constructor is used to initializing the private variables for class and Destructors to free the resources created /used by the class.
 
Here is a sample class with a constructor and destructor in PHP.
 
<?php
class Sample {
   
    private $name;
    private $link;

    public function __construct($name) {
        $this->name = $name;
    }

    public function setLink(Sample $link){
        $this->link = $link;
    }

    public function __destruct() {
        echo 'Destroying: '. $this->name;
    }
}
?>