पीएचपी ओओपी - वंशानुक्रम
PHP - वंशानुक्रम क्या है?
OOP में वंशानुक्रम = जब एक वर्ग दूसरे वर्ग से प्राप्त होता है।
चाइल्ड क्लास पैरेंट क्लास से सभी पब्लिक और प्रोटेक्टेड प्रॉपर्टीज और मेथड्स को इनहेरिट करेगा। इसके अलावा, इसके अपने गुण और तरीके हो सकते हैं।
extends
एक विरासत वर्ग को कीवर्ड का उपयोग करके परिभाषित किया गया है ।
आइए एक उदाहरण देखें:
उदाहरण
<?php
class Fruit {
public
$name;
public $color;
public
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
// Strawberry is inherited from Fruit
class
Strawberry extends Fruit {
public
function message() {
echo "Am I a fruit or a
berry? ";
}
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
उदाहरण समझाया गया
स्ट्रॉबेरी वर्ग को फल वर्ग से विरासत में मिला है।
इसका मतलब यह है कि स्ट्रॉबेरी वर्ग सार्वजनिक $name और $color गुणों के साथ-साथ विरासत के कारण फल वर्ग से सार्वजनिक __construct() और intro() विधियों का उपयोग कर सकता है।
स्ट्राबेरी वर्ग की भी अपनी विधि है: संदेश ()।
PHP - वंशानुक्रम और संरक्षित पहुँच संशोधक
पिछले अध्याय में हमने सीखा कि protected
गुणों या विधियों को कक्षा के भीतर और उस वर्ग से प्राप्त कक्षाओं द्वारा पहुँचा जा सकता है। इसका क्या मतलब है?
आइए एक उदाहरण देखें:
उदाहरण
<?php
class Fruit {
public
$name;
public $color;
public
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
class
Strawberry extends Fruit {
public
function message() {
echo "Am I a fruit or a
berry? ";
}
}
// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red");
// OK. __construct() is public
$strawberry->message(); // OK. message()
is public
$strawberry->intro(); // ERROR. intro()
is protected
?>
ऊपर के उदाहरण में हम देखते हैं कि यदि हम protected
कक्षा के बाहर से एक विधि (परिचय ()) को कॉल करने का प्रयास करते हैं, तो हमें एक त्रुटि प्राप्त होगी। public
तरीके ठीक काम करेंगे!
आइए एक और उदाहरण देखें:
उदाहरण
<?php
class Fruit {
public $name;
public
$color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
protected function intro() {
echo "The
fruit is {$this->name} and the color is {$this->color}.";
}
}
class Strawberry extends Fruit {
public function message() {
echo "Am I a fruit or a berry? ";
// Call protected
method from within derived class - OK
$this ->
intro();
}
}
$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is
public
$strawberry->message(); // OK. message() is
public and it calls intro() (which is protected) from within the
derived class
?>
ऊपर के उदाहरण में हम देखते हैं कि सब ठीक काम करता है! ऐसा इसलिए है क्योंकि हम
protected
व्युत्पन्न वर्ग के अंदर से विधि (परिचय ()) कहते हैं।
PHP - विरासत में मिली विधियों को ओवरराइड करना
चाइल्ड क्लास में विधियों (उसी नाम का उपयोग करें) को फिर से परिभाषित करके इनहेरिट की गई विधियों को ओवरराइड किया जा सकता है।
नीचे दिए गए उदाहरण को देखें। चाइल्ड क्लास (स्ट्रॉबेरी) में __construct () और इंट्रो () मेथड्स पैरेंट क्लास (फ्रूट) में __construct () और इंट्रो () मेथड्स को ओवरराइड कर देंगे:
उदाहरण
<?php
class Fruit {
public
$name;
public $color;
public
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name}
and the color is {$this->color}.";
}
}
class
Strawberry extends Fruit {
public $weight;
public
function __construct($name, $color, $weight) {
$this->name = $name;
$this->color = $color;
$this->weight = $weight;
}
public function intro() {
echo "The fruit is {$this->name}, the color is {$this->color},
and the weight is {$this->weight} gram.";
}
}
$strawberry = new Strawberry("Strawberry", "red",
50);
$strawberry->intro();
?>
पीएचपी - अंतिम कीवर्ड
कीवर्ड का final
उपयोग क्लास इनहेरिटेंस को रोकने या मेथड ओवरराइडिंग को रोकने के लिए किया जा सकता है।
निम्न उदाहरण दिखाता है कि वर्ग वंशानुक्रम को कैसे रोका जाए:
उदाहरण
<?php
final class Fruit {
// some code
}
//
will result in error
class
Strawberry extends Fruit {
// some code
}
?>
निम्न उदाहरण दिखाता है कि विधि ओवरराइडिंग को कैसे रोका जाए:
उदाहरण
<?php
class Fruit {
final public function intro() {
// some code
}
}
class
Strawberry extends Fruit {
//
will result in error
public function intro() {
// some code
}
}
?>