← Back to PHP Course | Chapter 8: OOP | Lesson 3 of 14

PHP Constructors & Destructors

A constructor is like the setup that happens when a new toy is built, such as putting in batteries. A destructor is the tidy-up that runs when the toy is thrown away.
Syntax
php
class ClassName {
    function __construct($parameter = default_value) {
        // runs when the object is created
    }

    function __destruct() {
        // runs when the object is destroyed
    }
}

$object = new ClassName($argument);

What is a Constructor?

A constructor is a special method named __construct() that PHP calls automatically the moment an object is created with new, making it the natural place to initialize required properties or validate input.

Example: What is a Constructor?

php
<?php
// Define the class `User`
class User {
    // Define the constructor `__construct` with no parameters
    function __construct() {
        // Print "User object created!" to the output
        echo "User object created!";
    }
}
new User();
?>

Constructors with Parameters

Constructor parameters let you require certain data up front, such as new User(Aditi, '[email protected]'), ensuring every object starts in a valid, fully-initialized state rather than needing manual setup afterward.

Example: Constructors with Parameters

php
<?php
// Define the class `User`
class User {
    // Declare the public property `$name`
    public $name;
    // Declare the public property `$email`
    public $email;
    // Define the constructor `__construct` taking `$name`, `$email`
    function __construct($name, $email) {
        $this->name = $name;
        $this->email = $email;
    }
}
// Create a new `User` instance with 'Aditi', '[email protected]', stored in `$user`
$user = new User('Aditi', '[email protected]');
// Print `$user->name . " " . $user->email` to the output
echo $user->name . " " . $user->email;
?>

Default Values in Constructors

A destructor is a special method named __destruct() that PHP calls automatically when an object is about to be destroyed, typically at the end of a script or when nothing references it anymore.

Example: Default Values in Constructors

php
<?php
// Define the class `Logger`
class Logger {
    // Define the magic method `__destruct` with no parameters
    function __destruct() {
        // Print "Logger destroyed" to the output
        echo "Logger destroyed";
    }
}
// Create a new `Logger` instance, stored in `$log`
$log = new Logger();
?>

What is a Destructor?

Destructors are most useful for cleanup tasks like closing a file handle or a database connection that the object opened, so resources don't leak even if the code that created the object forgets to close them explicitly.

Example: What is a Destructor?

php
<?php
// Define the class `FileHandler`
class FileHandler {
    // Define the magic method `__destruct` with no parameters
    function __destruct() {
        // Print "Closing file handle" to the output
        echo "Closing file handle";
    }
}
// Create a new `FileHandler` instance, stored in `$file`
$file = new FileHandler();
?>

Constructor and Destructor Together

Unlike constructors, destructors are rarely written explicitly in typical PHP web code, since PHP's request lifecycle already cleans up resources at the end of each request — they matter more in long-running scripts.

Example: Constructor and Destructor Together

php
<?php
// Define the class `Task`
class Task {
    // Define the constructor `__construct` with no parameters
    function __construct() {
        // Print "Task started\n" to the output
        echo "Task started\n";
    }
    // Define the magic method `__destruct` with no parameters
    function __destruct() {
        // Print "Task finished" to the output
        echo "Task finished";
    }
}
// Create a new `Task` instance, stored in `$task`
$task = new Task();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting to call parent::__construct() in a child class constructor, so the parent's setup does not run.
  2. Naming the constructor after the class as in old PHP 4 style, when modern PHP uses __construct.
  3. Expecting the destructor to run at a specific time, when it runs when the last reference goes away or the script ends.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.