HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Constructor & Destructor

In PHP, there are special "magic methods" that are automatically called at certain points in an object's lifecycle. The most important of these are the **Constructor** and the **Destructor**.


1. The PHP Constructor

A constructor allows you to initialize an object's properties when the object is created. It is defined using the __construct() magic method (with two underscores).

When you create an object with the new keyword, PHP automatically calls the __construct() function inside the class.

<?php
    class Fruit {
        public $name;
        public $color;

        // Constructor
        function __construct($name, $color) {
            $this->name = $name;
            $this->color = $color;
        }

        function get_fruit() {
            return "This is a " . $this->color . " " . $this->name;
        }
    }

    // Initialize the object with arguments
    $apple = new Fruit("Apple", "Red");
    echo $apple->get_fruit(); // Outputs: This is a Red Apple
?>
Tip: Using a constructor saves you from writing multiple set_name() and set_color() calls every time you create an object.

2. The PHP Destructor

A destructor is called when the object is destroyed or the script stops or exits. It is defined using the __destruct() magic method.

This is useful for cleaning up resources, such as closing a database connection or writing logs, just before the object is removed from memory.

<?php
    class Fruit {
        public $name;

        function __construct($name) {
            $this->name = $name;
            echo "Object created: " . $this->name . "<br>";
        }

        // Destructor
        function __destruct() {
            echo "Object destroyed: " . $this->name;
        }
    }

    $apple = new Fruit("Apple");
    // Script ends here, destructor will be called automatically
?>
Note: You cannot pass arguments to a destructor. It executes automatically when PHP's garbage collector decides the object is no longer needed.

Why Use Constructor & Destructor?

  • Automation: Constructors ensure that an object is always in a valid state from the moment it is created.
  • Cleanup: Destructors handle the "housekeeping" tasks automatically, reducing the risk of memory leaks or hanging connections.
  • Consistency: They provide a standard way to manage the start and end of an object's life.
Important: In PHP, magic methods start with a double underscore __. Never name your own custom functions with a double underscore prefix.

Summary

  • __construct() — Runs automatically when an object is created. Used for initialization.
  • __destruct() — Runs automatically when an object is destroyed. Used for cleanup.
  • Constructors can take arguments; destructors cannot.
  • Both are "magic methods" that help manage the object lifecycle.
Common Pitfall: Avoid placing heavy logic or time-consuming tasks in a destructor, as you cannot control exactly when it runs (especially in long-running CLI scripts).