← Back to PHP Course | Chapter 8: OOP | Lesson 5 of 14

PHP Inheritance

Inheritance एक बच्चे को माता-पिता से traits मिलने जैसा है। एक नई class automatically किसी दूसरी class से हर चीज़ reuse कर सकती है और फिर अपने खुद के extra skills add कर सकती है।
Syntax
php
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
<?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();
?>

Parent और Child Define करना

एक child class को automatically अपने parent के सभी public और protected members तक access मिलता है उन्हें दोबारा declare किए बिना, और यह अपनी inherited की गई चीज़ों के ऊपर नई properties या methods add कर सकती है।

उदाहरण: Defining Parent and Child

php
<?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();
?>

Protected Members को Inherit करना

parent::__construct() एक child class के constructor को explicitly अपने parent का constructor call करने देता है, यह ensure करते हुए कि child अपना खुद का setup logic add करने से पहले object का inherited हिस्सा सही से initialize हो जाए।

उदाहरण: Inheriting Protected Members

php
<?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;
?>

final Keyword

PHP classes के लिए सिर्फ single inheritance support करता है — एक class सिर्फ एक parent extend कर सकती है — हालाँकि यह कई interfaces implement कर सकती है, जो PHP का true multiple inheritance की ambiguity problems के आसपास काम करने का तरीका है।

उदाहरण: The final Keyword

php
<?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);
?>

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
<?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);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक method को parent से incompatible अलग signature से override करना, जो एक fatal error देता है।
  2. किसी class को inherit करने या final marked किसी method को override करने की कोशिश करना।
  3. unrelated classes के लिए inheritance इस्तेमाल करना, जबकि एक class को दूसरी को सिर्फ तभी extend करना चाहिए जब यह उसकी एक kind हो।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.