What are constructors and destructors in PHP?

Posted by Prem Tiwari | Updated on

The constructors and destructors in PHP are a special type of functions, which are automatically called when a PHP class object is created and destroyed, respectively.

While a constructor is used to initialize the private variables of a class, a destructor frees the resourced created or used by the class.

Here is a code example demonstrating the concept of constructors and destructors:

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers