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);
एक Constructor क्या है?
एक constructor __construct() नाम का एक special method है जिसे PHP किसी object के new से बनते ही automatically call करता है, इसे required properties initialize करने या input validate करने की natural जगह बनाते हुए।
उदाहरण: 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
Parameters के साथ Constructors
Constructor parameters आपको पहले से कुछ data की माँग करने देते हैं, जैसे new User(Aditi, '[email protected]'), यह ensure करते हुए कि हर object एक valid, पूरी तरह-initialized state में शुरू हो बजाय बाद में manual setup चाहिए होने के।
उदाहरण: 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
Constructors में Default Values
एक destructor __destruct() नाम का एक special method है जिसे PHP किसी object के destroy होने वाला होने पर automatically call करता है, आमतौर पर किसी script के अंत में या जब कोई भी इसे reference नहीं करता।
उदाहरण: 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
एक Destructor क्या है?
Destructors किसी file handle या database connection बंद करने जैसे cleanup tasks के लिए सबसे उपयोगी हैं जो object ने खोला था, ताकि resources leak न हों भले ही object बनाने वाला code उन्हें explicitly बंद करना भूल जाए।
उदाहरण: 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 और Destructor एक साथ
Constructors के उलट, destructors typical PHP web code में शायद ही explicitly लिखे जाते हैं, क्योंकि PHP का request lifecycle हर request के अंत में पहले से resources साफ कर देता है — वे long-running scripts में ज़्यादा मायने रखते हैं।
उदाहरण: 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
- किसी child class constructor में
parent::__construct()call करना भूल जाना, ताकि parent का setup न चले। - पुराने PHP 4 style की तरह constructor को class के नाम पर नाम देना, जबकि modern PHP
__constructइस्तेमाल करता है। - यह उम्मीद करना कि destructor एक specific समय पर चलेगा, जबकि यह तब चलता है जब last reference चला जाए या script खत्म हो।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: