PHP Constructor and Destructor

Summary: in this tutorial, you will learn how to initialize object’s properties using constructor and clean up resources before PHP releases the object from the memory using destructor.

PHP Constructor

When you create a new object, it is useful to initialize its properties e.g., for the bank account object you can set its initial balance to a specific amount. PHP provides you with a special method to help initialize object’s properties called constructor.

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

The following example adds a constructor to the BankAccount class that initialize account number and initial amount of money:

Now you can create a new bank account object with account number and initial amount as follows:

PHP constructor overloading

Constructor overloading allows you to create multiple constructors with the same name __construct() but different parameters. Constructor overloading enables you to initialize object’s properties in various ways. The following example demonstrates the idea of constructor overloading:

We have three constructors:

  • The first constructor is empty, it does nothing.
  • The second one only initializes account number.
  • The third one initializes both account number and initial amount.

PHP have not yet supported constructor overloading, Oops. Fortunately, you can achieve the same constructor overloading effect by using several PHP functions.

Let’s take a look at the following example:

How the constructor works.

  • First, we get constructor’s arguments using the func_get_args() function and also get the number of arguments using the  func_num_args() function.
  • Second, we check if the init_1() and init_2() method exists based on the number of constructor’s arguments using the  method_exists() function. If the corresponding method exists, we call it with an array of arguments using the call_user_func_array() function.

 PHP destructor

PHP destructor allows you to clean up resources before PHP releases the object from the memory. For example, you may create a file handle in the constructor and you close it in the destructor.

To add a destructor to a class, you just simply add a special method called __destruct() as follows:

There are some important notes regarding to the destructor:

  • Unlike a constructor, a destructor cannot accept any argument.
  • Object’s destructor is called before the object is deleted. It happens when there is no reference to the object or when the execution of the script is stopped by the exit() function.

The following simple FileUtil class demonstrates how to use the destructor to close a file handle. (Check it out the PHP File I/O tutorial to learn how to deal with files in PHP).

In this tutorial, we have introduced PHP constructor and destructor to help you initialize object’s properties and clean up resources before the object is deleted.