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.

Share:
Share on Facebook2

Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Leave a Reply

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

Most Popular Posts

How to change Base Path in Typo3

Posted on 4 years ago

Bhumi

Getting Started with CodeIgniter

Posted on 5 years ago

Bhumi

How to display XML content with PHP

Posted on 4 years ago

Bhumi

How do I add items in Top navigation bar?

Posted on 3 years ago

Bhumi

How to Use the HTML5 Full-Screen API

Updated 17 years ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 17 years ago

Bhumi

MySQL REGEXP Operator

Updated 17 years ago

Bhumi

HTML5 Geolocation API with Example

Updated 17 years ago

Bhumi

Overview of Views in MySQL

Updated 17 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 2 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 3 years ago

Bhumi