PHP Constructor and Destructor – Simple Examples

Welcome to a quick tutorial and examples of how to use the constructor and destructor in PHP. Just heard of this “constructor” and “destructor” thing and wonder what they do?

  • A constructor is a function that is called when an object is created.
    • class Demo { function __construct() { echo "This will run when a Demo object is created"; } }
    • $demo = new Demo();
  • A destructor is a function that is called when an object is destroyed.
    • class Demo { function __destruct() { echo "This will run when a Demo object is destroyed"; } }

That covers the basics, but read on for more examples!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Construct & Destruct Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

CONSTRUCTORS & DESTRUCTORS

All right, let us now get started with the examples of constructors and destructors in PHP.

 

1) BASIC CONSTRUCTOR & DESTRUCTOR

1-construct-destruct.php
<?php
class Demo {
  // (A) CONSTRUCTOR
  function __construct () {
    echo "Constructor is called.";
  }

  // (B) DESTRUCTOR
  function __destruct () {
    echo "Destructor is called.";
  }
}

// (C) NEW DEMO OBJECT
$demo = new Demo();

As in the above introduction, the idea of constructors and destructors is very easy:

  • A constructor is defined with function __construct (), and it will run when an object is created.
  • A destructor is defined with function __destruct (), and it will run when an object is destroyed.

Go ahead – Run the above example and see for yourself.

 

 

2) USER-DEFINED VS PREDEFINED CONSTRUCTOR

2-user-pre.php
<?php
class Demo {
  // (A) PREDEFINED CONSTRUCTOR - ONLY THIS WILL RUN
  function __construct () {
    echo "Predefined constructor is called.";
  }

  // (B) USER CONSTRUCTOR
  function Demo () {
    echo "User constructor is called.";
  }
}

// (C) NEW DEMO OBJECT
$demo = new Demo();

There are 2 ways to define a constructor.

  • The above function __construct () is called a “predefined constructor”.
  • The other way is to set a function name to be the same as the class name. This is called a “user-defined constructor”, but has been deprecated in PHP 8 – Do not use this method anymore.

Take note that when both of them exist in a single class, only the predefined construct will run.

 

3) PASSING PARAMETERS

3-parameters.php
<?php
class User {
  // (A) PROPERTIES
  private $name;
  private $email;
  
  // (B) CONSTRUCTOR
  function __construct ($name, $email) {
    $this->name = $name;
    $this->email = $email;
  }

  // (C) INTRODUCTION
  function intro() {
    echo "Hi! My name is ".$this->name.", my email is ".$this->email;
  }
}

// (D) CREATE OBJECT
$john = new User("John Doe", "john@doe.com");
$john->intro();

This shouldn’t be a mystery – Yes, we can pass parameters into the constructor just like calling any other functions.

 

 

4) INHERITANCE

4-inherit.php
<?php
// (A) USER CLASS
class User {
  // (A1) PROPERTIES
  protected $name;
  protected $email;

  // (A2) CONSTRUCTOR
  function __construct ($name, $email) {
    echo "User class constructor";
    $this->name = $name;
    $this->email = $email;
  }

  // (A3) INTRODUCTION
  function intro() {
    echo "Hi! My name is ".$this->name.", my email is ".$this->email;
  }
}

// (B) ADMIN CLASS
class Admin extends User {
  // (B1) PROPERTIES
  private $permission = "all";
}

// (C) NEW ADMIN OBJECT
$john = new Admin("John Doe", "john@doe.com");
$john->intro(); 

A quick recap for those who may have forgotten – A child class will inherit all of its parent’s properties and function. This also includes the constructor and destructor.

 

5) OVERRIDING

5-override.php
<?php
// (A) USER CLASS
class User {
  // (A1) PROPERTIES
  protected $name;
  protected $email;

  // (A2) CONSTRUCTOR
  function __construct ($name, $email) {
    echo "User class constructor";
    $this->name = $name;
    $this->email = $email;
  }

  // (A3) INTRODUCTION
  function intro() {
    echo "Hi! My name is ".$this->name.", my email is ".$this->email;
  }
}

// (B) ADMIN CLASS
class Admin extends User {
  // (B1) PROPERTIES
  private $permission = "all";
  
  // (B2) CONSTRUCTOR - WILL OVERRIDE PARENT
  function __construct ($name, $email) {
    echo "Admin class constructor";

    // BUT WE CAN STILL CALL THE PARENT'S CONSTRUCTOR
    parent::__construct($name, $email);
  }
  
  // (B3) INTRODUCTION
  function intro() {
    echo "I am ".$this->name.", I am an admin with ".$this->permission." permissions";
  }
}

// (C) NEW ADMIN OBJECT
$john = new Admin("John Doe", "john@doe.com");
$john->intro();

Finally, this is yet another recap. We can override the parent’s functions, and it applies to the constructor/destructor as well. To lessen the confusion:

  • If a child class does not have a defined constructor, it will inherit the parent’s constructor.
  • If a child class has a constructor, it will override the parent’s constructor.
  • But we can still use parent::__construct() to access it.

 

 

USEFUL BITS & LINKS

That’s all for this project, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

PHP Constructor & Destructor (Click To Enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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