Loading…

What are the __construct() and __destruct() methods in a PHP class?

Constructor and a Destructor both are special functions which are automatically called when an object is created and destroyed.

Example

                                        
class Animal
{

    public $name = "Hello";

    
    public function __construct($name)

    {

        echo "Live HERE";    

        $this->name = $name;

    }
    
    public function __destruct()

    {

        echo "Destroy here";

    }

}

$animal = new Animal("Bob");

echo "My Name is : " . $animal->name;

 

Suggest An Answer

No suggestions avaliable!

Latest post from PHP interview questions

Ask Question