PHP inheritance in hindi-php इनहेरिटेंस क्या है?

हेल्लो दोस्तों आज के इस पोस्ट में आपको PHP inheritance in hindi के बारे में बताया गया है की क्या होता है कैसे काम करता है तो चलिए शुरू करते है

Introduction to PHP Inheritance

Inheritance एक object oriented programming feature है। जिस प्रकार एक संतान अपने माता पिता की सम्पति को inherit (विरासत के रूप में प्राप्त) करती है, उसी प्रकार एक class किसी दूसरी class की properties और functions को inherit कर सकती है।

जिस class को inherit किया जाता है वह parent या base class कहलाती है। Inherit करने वाली class child या sub class कहलाती है। Child class parent class के properties और functions को use करने के आलावा खुद के भी properties और functions declare कर सकती है।

Inheritance को use करने के कई benefits है। Inheritance का सबसे महत्वपूर्ण benefit ये है की base class में declare की गयी किसी भी property या function को आप child class में use कर सकते है। इससे आपका समय तो बचता ही है साथ ही program में code भी कम हो जाता है जिससे program की complexity reduce हो जाती है।

Base class के members को access करने के लिए child class को उन्हें वापस declare या define करने की आवश्यकता नहीं होती है। जिस प्रकार कोई normal class अपनी properties और functions को access करती है, उसी प्रकार एक child class base class के members को access कर सकती है।

कोई भी child class किसी parent class के केवल public और protected members को ही access कर सकती है।

Types of Inheritance 

Inheritance के अलग अलग types के बारे में निचे बताया जा रहा है।  

  • Single Inheritance – इस तरह के inheritance में एक class को दूसरी class inherit करती है। इसे normal inheritance भी कहा जाता है। 
  • Multilevel Inheritance – इस तरह के inheritance में inherit करने वाली class को भी inherit किया जाता है। उदाहरण एक लिए यदि कोई class B है जो class A को inherit करती है, और कोई class C है जो class B को inherit करती है तो ऐसा inheritance multilevel inheritance कहलाता है। 
  • Multiple Inheritance – इस तरह के inheritance में एक class एक से अधिक classes को inherit करती है। इस प्रकार का inheritance PHP में allow नहीं है। 

PHP में multiple inheritance Interfaces द्वारा implement किया जा सकता है। एक class एक से अधिक interfaces को implement कर सकती है। 

Syntax of PHP Inheritance

PHP में किसी भी class को inherit करने का general syntax निचे दिया जा रहा है। 

class parentClass
{
   //properties & functions of parent class
}
class childClass extends parentClass
{
    //Can use properties & functions of parent class
   //properties & functions of child class
}

PHP में जब कोई class किसी दूसरी class को inherit करती है तो ऐसा वह extends keyword द्वारा करती है। जैसा की आप ऊपर दिए गए syntax में देख सकते है childClass extends keyword द्वारा parentClass को inherit कर रही है।

Example 

PHP में single inheritance को implement करने का simple उदाहरण निचे दिया जा रहा है।

<?php
class myClass
{
    public $name = "Inherited Variable";
}
class myOtherClass extends myClass
{
     public function displayName()
     {
         echo $name;
     }
}
?>


ऊपर दिए गए उदाहरण में myOtherClass द्वारा myClass को extend किया गया है। क्योंकि myOtherClass myClass को extend कर रही है इसलिए myOtherClass में myClass की public property $name को access करके print किया गया है। यह उदाहरण निचे दिया गया output generate करता है।

Overriding 

कई बार ऐसा हो सकता है की जो methods आपने parent class में define किये है उनकी definition आप child class में change करना चाहते है। ऐसा कई कारणों से हो सकता है जैसे की यदि किसी method की definition child class के according नहीं है तो आप उसे child class के अनुसार redefine कर सकते है। इस प्रकार वह method child class के लिए अलग तरीके से behave करेगा। 

जब parent class के किसी method को child class में change या redefine किया है तो वह process overriding कहलाती है।

Parent class के method को child class में override करने के लिए उसे child class में same signature के साथ दुबारा define किया जाता है। Same signature से यँहा मतलब same visibility mode, same method name और same argument list से है। इसके बाद आप definition को child class के according change करके लिख सकते है।

PHP में overriding को निचे उदाहरण द्वारा समझाया जा रहा है।

<?php
class parentClass
{
   public function display()
   {
       echo "This is parent class display.<br />";
   }
}

class childClass extends parentClass
{
    public function display()
    {
        echo "This child class display";
    }
}
$pObj = new parentClass();
 $pObj->display();

$cObj = new childClass();
$cObj->display();
?>


ऊपर दिए गए उदाहरण में parentClass के display function को childClass में override किया गया है। आप जिस class के object द्वारा display() function को call करना करेंगे उसी class का display() version call हो जाएगा। ये उदाहरण निचे दिया गया output generate करता है।

यदि आप childClass में parent class के function को call करना चाहते है तो इसके लिए आपको parent:: keyword use करना होगा। मान लीजिये आप parentClass के display function को base class में call करना चाहते है तो ऐसा आप इस प्रकार कर सकते है।

parent::display();


parent:: के द्वारा आप parent class के methods को access करते है।

Final 

जब आप चाहते है की base class के किसी method को override नहीं किया जा सके तो आप use final keyword के साथ declare करते है। Final keyword compiler को बताता है की यह method की final definition है और इसमें अब कोई change नहीं किया जा सकता है। इसका general syntax निचे दिया जा रहा है। 

finalKeyword visibilityMode functionKeyword functionName(arg-list);

मान लीजिये आप ऊपर दिए गए display function को parentClass में final declare करना चाहते है तो ऐसा आप इस प्रकार कर सकते है।

final public function display();

reference-https://www.tutorialspoint.com/php-object-inheritance

निवेदन-अगर आपको यह आर्टिकल(PHP inheritance in hindi) अच्छा लगा हो तो आप इस पोस्ट को अपने दोस्तों के साथ जरुर शेयर करे और आपको जिस टॉपिक(PHP inheritance in hindi) पर आपको पढना या नोट्स(PHP inheritance in hindi) चाहिए तो हमें जरुर कमेंट करे आपका कमेंट्स हमारे लिए बहु मूल्य है धन्यवाद

Leave a Comment