Files
developer-roadmap/roadmaps/php/content/constructor--destructor@oNUt1oT8pYBVvH0S2P6cb.md
Arik Chakma 274082fd77 feat: clean roadmap content and sync scripts (#10181)
* refactor: clean to roadmap content

* refactor: move shared helpers into scripts/lib
2026-07-28 05:45:17 +06:00

1.0 KiB

Constructor / Destructor

Constructor and Destructor methods are fundamental components of Object-Oriented Programming (OOP) in PHP. A constructor is a special type of method that automatically runs upon creating an object, often used to set property values or default behaviors. On the other hand, a destructor is a method that is automatically invoked when an object is due to be destroyed, perfect for cleanup activities. Here is a basic example:

class TestClass {
  public $value;

  // Constructor Method
  public function __construct($val) {
    $this->value = $val;
  }

  // Destructor Method
  public function __destruct() {
    echo "Object is being destroyed.";
  }
}

$obj = new TestClass("Hello World");
echo $obj->value; 
// Displays: Hello World
// And when the script ends, "Object is being destroyed."

Visit the following resources to learn more: