Certified PHP Developer Learning Resources Constructor and destructor

Learning Resources
 

Constructor and destructor


Constrcutor: Constructor methods for classes can be declared. Classes having a constructor method can call this method on each new object.

Syntax:
Void __ construct(arguments);

Example:
Class base {
      Function __constrcutor() {
               Print “In base class”;
      }
}

Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

Destructor: The destructor methods are called as soon as all references to a particular object are removed or when the object is explicitly destroyed.

Like constructors, parent destructors will not be called implicitly by the engine. In order to run a parent destructor, one would have to explicitly call parent::__destruct() in the destructor body.

Example:
Function __destruct()

 For Support