PHP Constructor

PHP

PHP Constructor: The __construct() Function

एक PHP Constructor आपको ऑब्जेक्ट के निर्माण पर ऑब्जेक्ट के properties को initialize करने की अनुमति देता है।

अगर हम __construct() फंक्शन क्रिएट करते हैं तो PHP उसे automatically कॉल कर देता है जैसे ही हम उस क्लास के ऑब्जेक्ट को बनाते हैं।

नोट: ध्यान दें कि construct फ़ंक्शन दो अंडरस्कोर ( __ ) से शुरू होता है ना की एक से।

नीचे हम एक उदाहरण देखेंगे जिसमें हम का construct फ़ंक्शन का इस्तेमाल नहीं करेंगे

Example-2: Without PHP Constructor
<?php

    // Class का निर्माण
    class Books{
        // Properties
        public $title;
        public $author;
        public $price;

        // Set Methods
        function setTitle($title){
            $this->title = $title;
        }
        function setAuthor($author){
            $this->author = $author;
        }
        function setPrice($price){
            $this->price = $price;
        }

        // Get Methods
        function getTitle(){
            return $this->title;
        }
        function getAuthor(){
            return $this->author;
        }
        function getPrice(){
            return $this->price;
        }
    }

    // Objects का निर्माण
    $maths = new Books();
    $physics = new Books();
    $finance = new Books();

    // Objects 1 का title, author, price सेट करना
    $maths->setTitle('Encyclopedias of Mathematics');
    $maths->setAuthor('James Stuart Tanton');
    $maths->setPrice('Rs.9,393');

    // Objects 2 का title, author, price सेट करना
    $physics->setTitle('Fundamentals of Physics');
    $physics->setAuthor('David Halliday, Jearl Walker, and Robert Resnick');
    $physics->setPrice('Rs.19,765');

    // Objects 3 का title, author, price सेट करना
    $finance->setTitle('Rich Dad Poor Dad');
    $finance->setAuthor('Robert T. Kiyosaki');
    $finance->setPrice(260);

    // Objects के itle, author, price को गेट करना
    echo $maths->getTitle() . "<br>" . $maths->getAuthor() . "<br>" . $maths->getPrice() . "<br><br>";
    echo $physics->getTitle() . "<br>" . $physics->getAuthor() . "<br>" . $physics->getPrice() . "<br><br>";
    echo $finance->getTitle() . "<br>" . $finance->getAuthor() . "<br>" . $finance->getPrice() . "<br><br>";
    ?>

Output
Encyclopedias of Mathematics
James Stuart Tanton
Rs.9,393

Fundamentals of Physics
David Halliday, Jearl Walker, and Robert Resnick
Rs.19,765

Rich Dad Poor Dad
Robert T. Kiyosaki
260

ऊपर के उदाहरण में हमने तीन ऑब्जेक्ट को कॉल किया और तीनों ऑब्जेक्ट के लिए set Methods का इस्तेमाल करना पड़ा ऑब्जेक्ट के प्रॉपर्टीज को सेट करने के लिए जैसे कि setTitle, setTitle और setPrice.

अब यहां पर अगर हम __construct फंक्शन का इस्तेमाल करते तो हमें set Methods को कॉल करने की जरूरत नहीं पड़ती जिससे हमें कोड के बहुत सारे लाइन लिखने से बच सकते हैं जिससे हमारा काफी समय बचेगा।

ऊपर दिए गए उदाहरण को ही हम दोबारा से __construct फंक्शन का इस्तेमाल करके बनाते हैं और फिर हम दोनों उदाहरण के अंतर को साफ-साफ देख सकेंगे।

Example-2: PHP Constructor using __construct() function

<?php
class Books{
    // Properties
    public $title;
    public $author;
    public $price;

    // PHP Constructor
    function __construct($title, $author, $price){
        $this->title = $title;
        $this->author = $author;
        $this->price = $price;
    }

    // Get Method
    function getTitle()    {
        return $this->title;
    }
    function getAuthor()    {
        return $this->author;
    }
    function getPrice()    {
        return $this->price;
    }
}

// Objects का निर्माण
$maths = new Books('Encyclopedias of Mathematics', 'James Stuart Tanton', 'Rs.9,393');
$physics = new Books('Fundamentals of Physics', 'David Halliday, Jearl Walker, and Robert Resnick', 'Rs.19,765');
$finance = new Books('Rich Dad Poor Dad', 'Robert T. Kiyosaki', 260);

// Objects के itle, author, price को गेट करना
echo $maths->getTitle() . "<br>" . $maths->getAuthor() . "<br>" . $maths->getPrice() . "<br><br>";
echo $physics->getTitle() . "<br>" . $physics->getAuthor() . "<br>" . $physics->getPrice() . "<br><br>";
echo $finance->getTitle() . "<br>" . $finance->getAuthor() . "<br>" . $finance->getPrice() . "<br><br>";
?>
Output
Encyclopedias of Mathematics
James Stuart Tanton
Rs.9,393

Fundamentals of Physics
David Halliday, Jearl Walker, and Robert Resnick
Rs.19,765

Rich Dad Poor Dad
Robert T. Kiyosaki
260

ऊपर दिए गए दोनों उदाहरणों को देखकर आप समझ सकते हैं कि __construct मेथड का इस्तेमाल करना हमारे लिए कितना फायदेमंद है, ऊपर हमने केवल 3 ऑब्जेक्ट को ही कॉल किया था, मान लीजिए अगर आपको हजारों ऑब्जेक्ट्स को कॉल करना हो तो कितने समय और कितने लाइंस ऑफ कोर्ट बचाया जा सकता है।


अब हमारे पास एक समस्या है मान लीजिए हमें किसी बुक का टाइटल तो पता है पर प्राइस नहीं पता तो अब स्थिति में हम क्या कर सकते हैं।

तो उसका बहुत ही सरल समाधान है अगर आप चाहते हैं कि किसी प्रॉपर्टी का वैल्यू नहीं भी डाला जाए तो भी हमें रिजल्ट प्रिंट हो, तो इसके लिए हम उस प्रॉपर्टी का डिफॉल्ट वैल्यू सेट करेंगे जो कि कुछ भी हो सकता है।

नीचे हम एक उदाहरण के माध्यम से देखते हैं।

Example: PHP Constructor कम प्रॉपर्टी के साथ
<?php
class Books{
    // Properties
    public $title;
    public $price;

    // PHP Constructor
    function __construct($title, $price = 0){
        $this->title = $title;
        $this->price = $price;
    }

    // Get Method
    function getTitle()    {
        return $this->title;
    }
    function getPrice()    {
        return $this->price;
    }
}

// Objects का निर्माण
$maths = new Books('Encyclopedias of Mathematics',  'Rs.9,393');
$physics = new Books('Fundamentals of Physics',  'Rs.19,765');
$finance = new Books('Rich Dad Poor Dad');

// Objects के itle, author, price को गेट करना
echo $maths->getTitle() . "<br>" . $maths->getPrice() . "<br><br>";
echo $physics->getTitle() . "<br>" . $physics->getPrice() . "<br><br>";
echo $finance->getTitle() . "<br>" . $finance->getPrice() . "<br><br>";
?>
Output
Encyclopedias of Mathematics
Rs.9,393

Fundamentals of Physics
Rs.19,765

Rich Dad Poor Dad
0

ऊपर दिए गए उदाहरण में आप देखेंगे तो पाएंगे कि $finance ऑब्जेक्ट में हमने केवल Title पास किया है और उसका Price नहीं डाला है इस स्थिति में Price के डिफॉल्ट वैल्यू जो कि हमने __construct मेथड में 0 सेट किया है वह प्रिंट हो जाएगा।

Reference: https://www.php.net/manual/en/language.oop5.decon.php

Article By: Brajlal Prasad
Created on: 22 Apr 2023  977  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]