Constructor क्या है?
Constructor एक विशेष function है जो object को create करते समय अपने आप call होता है। इसका इस्तेमाल object के शुरुआती state को set करने के लिए किया जाता है।
दूसरे शब्दों में कहें तो, “Constructor एक विशेष प्रकार का method होता है, जो अपने आप call होता है जब हम किसी class के object को create करते हैं। Constructor का मुख्य उद्देश्य object को initialize करना, यानी object के properties को initial values देना होता है।”
PHP में constructor को construct() नाम से define किया जाता है। यह double underscore (_ _) से शुरू होता है।
Constructor का उदाहरण:
<?php
class Student {
public $name;
public $age;
// Constructor
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function displayInfo() {
echo "Name: " . $this->name . ", Age: " . $this->age;
}
}
// Object creation (Constructor automatically called)
$student1 = new Student("Rahul", 20);
$student1->displayInfo(); // Output: Name: Rahul, Age: 20
?>
ऊपर दिए गए उदाहरण में, जब हम new Student(“Rahul”, 20) लिखते हैं, तो __construct() method अपने आप call हो जाता है। $this->name और $this->age का इस्तेमाल करके object के properties को initialize किया जाता है।
Destructor क्या है?
Destructor एक विशेष function होता है जो object के destruction के समय अपने आप call होता है। इसका उपयोग resource को release करने या cleanup operations करने के लिए किया जाता है।
दूसरे शब्दों में कहें तो, “Destructor भी एक विशेष प्रकार का method होता है, जो अपने आप call होता है जब object का काम खत्म हो जाता है या object destroy हो जाता है। Destructor का मुख्य उद्देश्य resources को free करना, जैसे database connections बंद करना या memory को clean करना होता है।”
Destructor का नाम हमेशा __destruct कीवर्ड से define किया जाता है। यह भी double underscore (_ _) से शुरू होता है।
Destructor का उदाहरण:
<?php
class Student {
public $name;
public $age;
// Constructor
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
echo "Object created for " . $this->name . "<br>";
}
// Destructor
public function __destruct() {
echo "Object destroyed for " . $this->name . "<br>";
}
public function displayInfo() {
echo "Name: " . $this->name . ", Age: " . $this->age . "<br>";
}
}
// Object creation
$student1 = new Student("Rahul", 20);
$student1->displayInfo();
// Object creation
$student2 = new Student("Priya", 22);
$student2->displayInfo();
// Output:
// Object created for Rahul
// Name: Rahul, Age: 20
// Object created for Priya
// Name: Priya, Age: 22
// Object destroyed for Priya
// Object destroyed for Rahul
?>
जब script का execution खत्म हो जाता है, तो destructor अपने आप call हो जाता है। Destructor का इस्तेमाल clean-up tasks के लिए किया जाता है।
Constructor और Destructor के बीच अंतर (Difference Between Constructor and Destructor in Hindi)
Constructor (__construct()) | Destructor (__destruct()) |
इसमें Object create होते ही call होता है। | इसमें Object destroy होते ही call होता है। |
इसे Object को initialize करने के लिए use किया जाता है। | इसे Resources को free करने के लिए use किया जाता है। |
यह Parameters accept कर सकता है। | यह Parameters accept नहीं करता है। |
इसे पढ़ें:-
निवेदन:- मुझे उम्मीद है कि यह पोस्ट आपके लिए उपयोगी रही होगी, इसे अपने दोस्तों के साथ share कीजिए। और आपके जो भी questions हैं उन्हें नीचे comment करके पूछिए।