In object oriented programming terminology, constructor is a method defined inside a class that is called automatically at the time of creation of the object. Purpose of a constructor method is to initialise the object. In PHP, a method of special name __construct acts as a constructor.

__construct ([ mixed $args = “” [, $… ]] ) : void

Example

<?php

class myclass{

   function __construct(){

      echo “object initialised”;

   }

}

$obj=new myclass();

?>

Output

This will produce the following result. −

object initialised

Destructor

Destructor is a method automatically as soon as a garbage collector finds that a particular object has no more references. In PHP, the destructor method is named as __destruct. During the shutdown sequence too, objects will be destroyed. Destructor method doesn’t take any arguments, neither does it return any data type

<?php

class myclass{

   function __construct(){

      echo “object is initialised

“;

   }

   function __destruct(){

      echo “object is destroyed

“;

   }

}

$obj=new myclass();

?>

Output

This will show following result

object is initialised

object is destroyed