Constructors and Destructors
__construct()
Section titled “__construct()”A magic method that is automatically called when an object is instantiated. Used to initialize properties or perform setup tasks.
<?phpclass User { public $name; public $email;
// Constructor method public function __construct($name, $email) { $this->name = $name; $this->email = $email; echo "User object created for: $name" . PHP_EOL; }}
$user = new User("John", "john@test.com"); // Output: User object created for: Johnecho $user->name . PHP_EOL; // Output: John__destruct()
Section titled “__destruct()”A magic method that is automatically called when an object is destroyed or script ends. Used for cleanup tasks like closing files or database connections.
<?phpclass FileHandler { private $file;
public function __construct($filename) { $this->file = fopen($filename, 'w'); echo "File opened" . PHP_EOL; }
// Destructor method public function __destruct() { fclose($this->file); echo "File closed" . PHP_EOL; }}
$handler = new FileHandler("test.txt"); // Output: File opened// When script ends or object is destroyed: File closed