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

PHP Constructors और Destructors

एक constructor उस setup जैसा है जो एक नया toy बनते समय होता है, जैसे batteries डालना। एक destructor वह tidy-up है जो toy फेंके जाने पर चलता है।
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);

एक Constructor क्या है?

एक constructor __construct() नाम का एक special method है जिसे PHP किसी object के new से बनते ही automatically call करता है, इसे required properties initialize करने या input validate करने की natural जगह बनाते हुए।

उदाहरण: 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();
?>

Parameters के साथ Constructors

Constructor parameters आपको पहले से कुछ data की माँग करने देते हैं, जैसे new User(Aditi, '[email protected]'), यह ensure करते हुए कि हर object एक valid, पूरी तरह-initialized state में शुरू हो बजाय बाद में manual setup चाहिए होने के।

उदाहरण: 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;
?>

Constructors में Default Values

एक destructor __destruct() नाम का एक special method है जिसे PHP किसी object के destroy होने वाला होने पर automatically call करता है, आमतौर पर किसी script के अंत में या जब कोई भी इसे reference नहीं करता।

उदाहरण: 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();
?>

एक Destructor क्या है?

Destructors किसी file handle या database connection बंद करने जैसे cleanup tasks के लिए सबसे उपयोगी हैं जो object ने खोला था, ताकि resources leak न हों भले ही object बनाने वाला code उन्हें explicitly बंद करना भूल जाए।

उदाहरण: 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 और Destructor एक साथ

Constructors के उलट, destructors typical PHP web code में शायद ही explicitly लिखे जाते हैं, क्योंकि PHP का request lifecycle हर request के अंत में पहले से resources साफ कर देता है — वे long-running scripts में ज़्यादा मायने रखते हैं।

उदाहरण: 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. #}
आम गलतियां
  1. किसी child class constructor में parent::__construct() call करना भूल जाना, ताकि parent का setup न चले।
  2. पुराने PHP 4 style की तरह constructor को class के नाम पर नाम देना, जबकि modern PHP __construct इस्तेमाल करता है।
  3. यह उम्मीद करना कि destructor एक specific समय पर चलेगा, जबकि यह तब चलता है जब last reference चला जाए या script खत्म हो।

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.