- Introduction to PHP inheritance in Hindi
- Types of PHP inheritance in Hindi
- Syntax of 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 |
PHP में जब कोई class किसी दूसरी class को inherit करती है तो ऐसा वह extends keyword द्वारा करती है। जैसा की आप ऊपर दिए गए syntax में देख सकते है childClass extends keyword द्वारा parentClass को inherit कर रही है।
Example
PHP में single inheritance को implement करने का simple उदाहरण निचे दिया जा रहा है।
<?php
// Parent class // Child class public function displayName() } ?> |
ऊपर दिए गए उदाहरण में 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
// Parent class public function display() } // Child class // Overriding parent class function } // Parent class object // Parent class display() function // Child class object // Child class display() function ?> |
ऊपर दिए गए उदाहरण में 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();
|
Previous: PHP Abstract Classes
Next: PHP Interfaces