PHP Inheritance
In this page:
class ParentClass {
// properties and methods
}
class ChildClass extends ParentClass {
// inherits from ParentClass, adds its own
}
Inheritance क्या है?
Inheritance एक class (child) को extends keyword इस्तेमाल करके दूसरी class (parent) की properties और methods reuse करने देता है, related classes में shared logic को दोहराने से बचते हुए।
उदाहरण: What is Inheritance?
<?php
// Define the class `Animal`
class Animal {
// Define the function `eat` with no parameters
function eat() {
// Print "Eating...\n" to the output
echo "Eating...\n";
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->eat();
?>
Login to try C/C++/Java/PHP code in the editor
Parent और Child Define करना
एक child class को automatically अपने parent के सभी public और protected members तक access मिलता है उन्हें दोबारा declare किए बिना, और यह अपनी inherited की गई चीज़ों के ऊपर नई properties या methods add कर सकती है।
उदाहरण: Defining Parent and Child
<?php
// Define the class `Animal`
class Animal {
// Declare the public property `$name`, set to "Generic Animal"
public $name = "Generic Animal";
// Define the function `eat` with no parameters
function eat() {
// Print "Eating\n" to the output
echo "Eating\n";
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
// Define the function `bark` with no parameters
function bark() {
// Print "Barking\n" to the output
echo "Barking\n";
}
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->eat();
$dog->bark();
?>
Login to try C/C++/Java/PHP code in the editor
Protected Members को Inherit करना
parent::__construct() एक child class के constructor को explicitly अपने parent का constructor call करने देता है, यह ensure करते हुए कि child अपना खुद का setup logic add करने से पहले object का inherited हिस्सा सही से initialize हो जाए।
उदाहरण: Inheriting Protected Members
<?php
// Define the class `Animal`
class Animal {
// Declare the public property `$name`
public $name;
// Define the constructor `__construct` taking `$name`
function __construct($name) {
$this->name = $name;
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
// Define the constructor `__construct` taking `$name`
function __construct($name) {
parent::__construct($name);
}
}
// Create a new `Dog` instance with "Rex", stored in `$dog`
$dog = new Dog("Rex");
// Print `$dog->name` to the output
echo $dog->name;
?>
Login to try C/C++/Java/PHP code in the editor
final Keyword
PHP classes के लिए सिर्फ single inheritance support करता है — एक class सिर्फ एक parent extend कर सकती है — हालाँकि यह कई interfaces implement कर सकती है, जो PHP का true multiple inheritance की ambiguity problems के आसपास काम करने का तरीका है।
उदाहरण: The final Keyword
<?php
// Define the interface `Swims` describing required methods
interface Swims {}
// Define the interface `Flies` describing required methods
interface Flies {}
// Define the class `Duck`, implementing `Swims, Flies`
class Duck implements Swims, Flies {
}
// Create a new `Duck` instance, stored in `$duck`
$duck = new Duck();
// Print a detailed dump (with types) of `$duck instanceof Swims`
var_dump($duck instanceof Swims);
?>
Login to try C/C++/Java/PHP code in the editor
Web Tools के लिए Inheritance
एक अच्छा inheritance relationship एक genuine is-a relationship model करता है, जैसे Dog extends Animal, सिर्फ unrelated code reuse करने के लिए इस्तेमाल होने के बजाय, जो fragile, confusing class hierarchies produce करता है।
उदाहरण: Inheritance for Web Tools
<?php
class Animal {}
class Dog extends Animal {}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
// Print a detailed dump (with types) of `$dog instanceof Animal`
var_dump($dog instanceof Animal);
?>
Login to try C/C++/Java/PHP code in the editor
- एक method को parent से incompatible अलग signature से override करना, जो एक fatal error देता है।
- किसी class को inherit करने या
finalmarked किसी method को override करने की कोशिश करना। - unrelated classes के लिए inheritance इस्तेमाल करना, जबकि एक class को दूसरी को सिर्फ तभी extend करना चाहिए जब यह उसकी एक kind हो।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: