PHP Inheritance

PHP

What is PHP Inheritance?

Inheritance इसका मतलब है विरासत, यानी कि किसी क्लास के पब्लिक एवं प्रोटेक्टेड प्रॉपर्टी ओर मेथड का इस्तेमाल करना

इस को सरल भाषा में समझते हैं, जैसे की हम अपने पिता के गाड़ी को अपने लिए इस्तेमाल कर सकते हैं ओर इसके अलावा हम अपने गाड़ी का भी इस्तेमाल कर सकते हैं

उसी तरह PHP में child class अपने parent class के सभी public और protected properties और methods को से प्राप्त करता है और उसका इस्तेमाल करता है। इसके अलावा, इसके अपने properties और methods भी हो सकती हैं।

PHP में extends कीवर्ड का उपयोग कर एक inherited class को परिभाषित किया जाता है।

  • यह hierarchical वर्गीकरण की सिद्धांत को फॉलो करता है।
  • Inheritance के तीन प्रकार होते हैं, single, multiple और multilevel Inheritance
  • PHP केवल single inheritance को फॉलो करता है, जहाँ single parent class से केवल एक class प्राप्त किया जा सकता है।
उदाहरण: PHP Inheritance
<?php
class Animal{
    public function run(){
        echo "Animal can run ";
    }
}

class Dog extends Animal{
    public function eat(){
        echo "It can eat";
    }
}
$obj = new Dog();
$obj->run();
echo " and ";
$obj->eat();
?>  
Output
Animal can run and It can eat
उदाहरण: PHP Inheritance
<?php
class Books{
    public $title;
    public $author;
    public function __construct($title, $author){
        $this->title = $title;
        $this->author = $author;
    }
    public function bookInfo(){
        echo "The book is {$this->title} and the author is {$this->author}";
    }
}

// Mathematics is inherited from Books
class Mathematics extends Books{
    public function message(){
        echo "This is a Maths book. </br> ";
    }
}
$mathematics = new Mathematics("Encyclopedias of Mathematics ", "James Stuart Tanton</br>");
$mathematics->message();
$mathematics->bookInfo();
?>
Output
This is a Maths book.
The book is Encyclopedias of Mathematics and the author is James Stuart Tanton

उद्धारण को समझते हैं

  • उपरोक्त उदाहरण में Mathematics क्लास ने Books क्लास को inherit किया है।
  • जिसका मतलब यह है कि Mathematics क्लास अब Books क्लास के पब्लिक प्रॉपर्टी $title और $author का इस्तेमाल कर सकता है, इसके साथ साथ पब्लिक फंक्शन bookInfo और पब्लिक __construct का भी इस्तेमाल कर सकता है।
  • Mathematics क्लास का अपना मेथड भी हो सकता है जैसे कि message().

PHP Inheritance - Access Modifiers

पिछले अध्याय PHP - Access Modifiers में हमने देखा है कि protected properties या methods को class के अन्दर और उस class के subclass द्वारा access किया जा सकता है। इसका क्या मतलब है यह हम नीचे एक उदाहरण के माध्यम से समझने की कोशिश करेंगे।

उदाहरण: PHP Inheritance Protected Access Modifier
<?php
class Books{
    public $title;
    public $author;
    public function __construct($title, $author){
        $this->title = $title;
        $this->author = $author;
    }
    protected function bookInfo(){
        echo "The book is {$this->title} and the author is {$this->author}";
    }
}

// Mathematics is inherited from Books
class Mathematics extends Books{
    public function message(){
        echo "This is a Maths book. </br> ";
    }
}
$mathematics = new Mathematics("Encyclopedias of Mathematics ", "James Stuart Tanton</br>");
$mathematics->message();
$mathematics->bookInfo(); // Uncaught Error: Call to protected method Books
?>

उपरोक्त उदाहरण में हमने एक protected मेथड को उस क्लास के बाहर से कॉल करने की कोशिश की और हमें error देखने को मिला जबकि पब्लिक मेथड अच्छे से काम कर रहा है।

नीचे एक दूसरे उदाहरण को देखते और समझने की कोशिश करते हैं।

उदाहरण: PHP Inheritance
<?php
class Books{
    public $title;
    public $author;
    public function __construct($title, $author){
        $this->title = $title;
        $this->author = $author;
    }
    protected function bookInfo(){
        echo "The book is {$this->title} and the author is {$this->author}";
    }
}

// Mathematics is inherited from Books
class Mathematics extends Books{
    public function message(){
        echo "This is a Maths book. </br> ";
        $this->bookInfo(); // Protected function
    }
}
$mathematics = new Mathematics("Encyclopedias of Mathematics ", "James Stuart Tanton</br>");
$mathematics->message();
?>
Output
This is a Maths book.
The book is Encyclopedias of Mathematics and the author is James Stuart Tanton

उपरोक्त उदाहरण में हमने प्रोटेक्टेड मेथड bookInfo का इस्तेमाल किया और यह अच्छे से काम कर रहा है, ऐसा इसलिए क्योंकि हमने प्रोटेक्टेड मेथड को उस class (Book) के subclass (Mathematics) के अंदर इस्तेमाल किया जोकि Book क्लास को इन्हेरीटेड कर रहा है।

PHP - The final Keyword

अगर आप चाहते हो किसी क्लास को इनहेरिटेंस ना किया जाए तो आप final कीवर्ड का इस्तेमाल कर सकते हैं, final कीवर्ड का उपयोग क्लास इनहेरिटेंस को रोकने या मेथड ओवरराइडिंग को रोकने के लिए किया जा सकता है।

उदाहरण: The final Keyword
<?php
final class Books{
   // Codes
}

class Mathematics extends Books{
    // Codes
}
?>

उपरोक्त उदाहरण में जैसे ही हम Book क्लास के आगे final कीवर्ड को लगाते हैं और सबक्लास (Mathematics) द्वारा Book क्लास को extends यानि की इनहेरिटेंस करने की कोशिश करते हैं तो हमें एक error दिखाई देता हैं। Class Mathematics cannot extend final class Books.

Ref: www.php.net

Article By: Brajlal Prasad
Created on: 09 Jun 2023  861  Views
 Print Article
Report Error

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