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

PHP Method Overriding

Method overriding तब होती है जब एक child अपने तरीके से same काम करती है, किसी बच्चे जैसे जिसे एक तरीके से shoes tie करना सिखाया गया और वह अलग तरीके से करने का decide करता है। Child का version parent के version की जगह ले लेता है।
Syntax
php
class ParentClass {
    function method_name() { }
}

class ChildClass extends ParentClass {
    function method_name() {
        parent::method_name();   // optional: call the parent version
    }
}

Method Overriding क्या है?

Method overriding तब होती है जब एक child class एक method को फिर से define करती है जो इसकी parent class के पास पहले से है, किसी child object पर call होने पर parent के behavior को अपने version से replace करते हुए।

उदाहरण: What is Method Overriding?

php
<?php
// Define the class `Animal`
class Animal {
    // Define the function `speak` with no parameters
    function speak() {
        // Print "Some sound" to the output
        echo "Some sound";
    }
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
    // Define the function `speak` with no parameters
    function speak() {
        // Print "Bark!" to the output
        echo "Bark!";
    }
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->speak();
?>

parent Keyword

PHP runtime पर actual object की class के आधार पर decide करता है कि किसी overridden method का कौन सा version चलेगा, इसे hold करने वाले variable के type के आधार पर नहीं, जो polymorphic code को अलग-अलग subclasses को uniformly treat करने देता है।

उदाहरण: The parent Keyword

php
<?php
// Define the class `Shape`
class Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `0` from this function
        return 0;
    }
}
// Define the class `Square`, inheriting from `Shape`
class Square extends Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `4` from this function
        return 4;
    }
}
// Create a new `Square` instance, stored in `$shape`
$shape = new Square();
// Print `$shape->area()` to the output
echo $shape->area();
?>

Constructors को Override करना

एक overriding method के अंदर parent::methodName() आपको parent की original implementation call करने और इसे extend करने देता है, इसे पूरी तरह replace करने के बजाय — तब उपयोगी जब child को inherited logic से पहले या बाद में behavior add करना हो।

उदाहरण: Overriding Constructors

php
<?php
// Define the class `Animal`
class Animal {
    // Define the function `speak` with no parameters
    function speak() {
        // Print "Generic sound\n" to the output
        echo "Generic sound\n";
    }
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
    // Define the function `speak` with no parameters
    function speak() {
        parent::speak();
        // Print "Bark!" to the output
        echo "Bark!";
    }
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->speak();
?>

final से Overriding रोकना

एक overriding method का signature (parameter types और visibility) आमतौर पर parent के साथ compatible रहना ज़रूरी है, और PHP कुछ incompatible changes के लिए errors raise करता है, खासकर typed parameters के साथ।

उदाहरण: Preventing Overriding with final

php
<?php
// Define the class `Animal`
class Animal {
    // Define the function `speak` taking `string $sound`
    function speak(string $sound) {
        // Print `$sound` to the output
        echo $sound;
    }
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
    // Define the function `speak` taking `string $sound`
    function speak(string $sound) {
        // Print `strtoupper($sound)` to the output
        echo strtoupper($sound);
    }
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->speak("bark");
?>

cookiescursor.com पर Overriding

Overriding polymorphism के लिए central है: एक Shape base class एक generic area() method define कर सकती है जिसे Circle और Square हर एक अपने सही formula से override करते हैं, calling code को यह परवाह किए बिना बस ->area() call करने देते हुए कि shape कौन सा है।

उदाहरण: Overriding on cookiescursor.com

php
<?php
// Define the class `Shape`
class Shape {
    // Define the function `area` with no parameters
    function area() {
        // Return `0` from this function
        return 0;
    }
}
// Define the class `Circle`, inheriting from `Shape`
class Circle extends Shape {
    // Declare the private property `$radius`
    private $radius;
    // Define the constructor `__construct` taking `$radius`
    function __construct($radius) {
        $this->radius = $radius;
    }
    // Define the function `area` with no parameters
    function area() {
        // Return `3.14 * $this->radius * $this->radius` from this function
        return 3.14 * $this->radius * $this->radius;
    }
}
// Define the class `Square`, inheriting from `Shape`
class Square extends Shape {
    // Declare the private property `$side`
    private $side;
    function __construct($side) {
        $this->side = $side;
    }
    function area() {
        return $this->side * $this->side;
    }
}
$shapes = [new Circle(2), new Square(3)];
foreach ($shapes as $shape) {
    echo $shape->area() . "\n";
}
?>
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. एक overriding method की parameter list बदलना ताकि यह parent के signature के साथ अब compatible न रहे।
  2. parent::method() भूल जाना जब parent का behavior अभी भी चलना चाहिए था।
  3. एक final method को override करने की कोशिश करना, जो एक fatal error है।

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.