Back to Top

Constructor and Destructor in PHP

Constructor and Destructor in PHP

What is Constructor?

Generally Constructor is one kind of method which has the same name as class name.it’s used to initialize the variable of the object at the time of declaration.The constructor is basically OOPs concept in PHP.You also like Type Hinting in PHP5 and PHP Magical Constants.

In PHP4, Constructor is the method which have the same name as class name whereas in PHP5, We have magic method say __construct to call the constructor.The constructor gets called automatically for each object that has got created.It’s may or may not take parameters.

If you declare a function in a class named with two underscores (__) like __construct, this function will be treated as a constructor in PHP when creating an object instance of the class.Like any other function, the constructor can have parameters or default values.

  • Constructor accept parameters, can be assigned to object properties at object creation
  • The constructor can call the class method or other functions
  • Constructor can calls constructors of other classes

NOTE: Constructor should be defined in PUBLIC class.

Basic Example

Note: If the child class defines a constructor is not an implicitly call the constructor of its parent class. To execute the constructor of the parent class, call parent in the constructor of the parent::__construct();

Parameterized Constructor

Parameterized constructor means we pass parameter within in constructor method.Constructor can take arguments so

Let’s see by example.

NOTE: The constructor is non-static member function.

What is Destructor?

The destructor gets called for each object of the class that is destroyed.It appears as the member function of each class whether it’s defined or not.For destructor, we also have the magic method called __destruct in PHP.

  • Destructor is used to destroy the object at the time, automatic call, the call can not be displayed.
  • Destructors can not have parameters.

The destructor can use to guarantee a proper deinitialize the object of the class and free up the resources acquired by the object.The destructor is also a member function of a class.

Now let’s see mysql connect code which uses both Constructor and destructor.

NOTE: The destructor of subclass will not be called implicitly of the parent class, to execute the parent class destructor must call the parent destructor parent::__ destruct().

Here, In above code includes the definition for the destructor function. I have explicitly closed mysql connection using the __destruct function.When the destructor is called, and the object itself is destroyed.

By the way, are you using destructor in your application? Share your viewpoints in the comment section below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Objects and Classes in PHP

Posted on 7 years ago

Bhumi

How to access Remote File using PHP

Posted on 12 years ago

Bhumi

How to use CONST keyword in PHP

Posted on 12 years ago

Bhumi