PHP Constructors & Destructors
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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;
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting to call
parent::__construct()in a child class constructor, so the parent's setup does not run. - Naming the constructor after the class as in old PHP 4 style, when modern PHP uses
__construct. - Expecting the destructor to run at a specific time, when it runs when the last reference goes away or the script ends.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: