PHP Constructors and Destructors


PHP Constructors

PHP Constructors are special member functions for initial settings from a class of newly generated object instances, which in PHP5 is the core part of the object-oriented framework. Builders are the essential building blocks that describe the potential entity and its existence. You may assume that the builders are plans for constructing structures that have values for the membership functions and variables. The constructor is immediately named until the model has been initialized.

The constructor is an integral part of the definition of PHP oop. Constructor in PHP is a special type of function of a class that is automatically executed as it creates or instantiates some object of that class. A constructor is also called magic function since the magic method typically starts with two underscore characters in PHP.

You simply add a special method with the name __construct() to add a constructor to a class. If you create a new object, PHP automatically searches for the method and calls it.


function __construct()
// The object and its properties are initialized by assigning

PHP Constructors

Forms of constructors:

Default Constructor: There are no parameters, but the values can be dynamically passed on to the default constructor.
Parameterized Constructor: The parameters are needed, and you can pass different values on to the members of the data.
Copy Constructor: This parameter accepts the address of the other objects.

PHP Destructors

Destructor is also a special member function that is essentially the inverse of the constructor form that is named when the memory deletes an instance of the class. Destructors (__destruct ( void): void) are methods that are named when there is no connection to any class entity, or when they go out of control or are specifically about to be published.

We have no forms nor return interest. It is named just before de-allocating memory for an entity or when the execution of PHP scripts is over, or as soon as the execution control leaves the stack. When the complete script or code ends, global artifacts are lost. Cleaning of resources happens in the destructor process until memory release or deletion of directories, where they are no longer used in the application. PHP Garbage Collector performs the automated removal of class-objects.

The Destructor approach should be named as soon as there are no other references to a single entity during the shutdown series or in some order. Destructor must at last call automatically. Note: Uses __destruct() to describe destructor.


function __destruct()
// destroying or cleaning up the object

There are few significant remarks about the destroyer:

Unlike a constructor, no statement can be supported by a destructor.
The destructor of the entity is named before removing the entity. It occurs when there is no object relation, or when the exit() function prevents the execution of the script.