PHP Method Overriding
In this page:
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
// 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();
?>
Login to try C/C++/Java/PHP code in the editor
parent Keyword
PHP runtime पर actual object की class के आधार पर decide करता है कि किसी overridden method का कौन सा version चलेगा, इसे hold करने वाले variable के type के आधार पर नहीं, जो polymorphic code को अलग-अलग subclasses को uniformly treat करने देता है।
उदाहरण: The parent Keyword
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
Constructors को Override करना
एक overriding method के अंदर parent::methodName() आपको parent की original implementation call करने और इसे extend करने देता है, इसे पूरी तरह replace करने के बजाय — तब उपयोगी जब child को inherited logic से पहले या बाद में behavior add करना हो।
उदाहरण: Overriding Constructors
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
final से Overriding रोकना
एक overriding method का signature (parameter types और visibility) आमतौर पर parent के साथ compatible रहना ज़रूरी है, और PHP कुछ incompatible changes के लिए errors raise करता है, खासकर typed parameters के साथ।
उदाहरण: Preventing Overriding with final
<?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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
- एक overriding method की parameter list बदलना ताकि यह parent के signature के साथ अब compatible न रहे।
parent::method()भूल जाना जब parent का behavior अभी भी चलना चाहिए था।- एक
finalmethod को override करने की कोशिश करना, जो एक fatal error है।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: