PHP Inheritance
PHP inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. The class that inherits is called the child or subclass, while the class being inherited from is called the parent or superclass.
Here’s an explanation with examples to illustrate PHP inheritance:
Basic Example
Parent Class
<?php
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function makeSound() {
return "Some generic sound";
}
}
?>
Child Class
<?php
class Dog extends Animal {
public function makeSound() {
return "Bark";
}
}
$dog = new Dog("Buddy");
echo $dog->name; // Outputs: Buddy
echo $dog->makeSound(); // Outputs: Bark
?>
In this example:
- The
Animalclass is the parent class with a property$nameand a methodmakeSound(). - The
Dogclass is the child class that extendsAnimal. It inherits the$nameproperty and themakeSound()method, but it overrides themakeSound()method to return "Bark". - When we create an instance of
Dog, we can access the inherited$nameproperty and the overriddenmakeSound()method.
Using parent Keyword
The parent keyword allows the child class to call methods and access properties of its parent class.
Modified Example
<?php
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function makeSound() {
return "Some generic sound";
}
}
class Dog extends Animal {
public function makeSound() {
return parent::makeSound() . " and Bark";
}
}
$dog = new Dog("Buddy");
echo $dog->makeSound(); // Outputs: Some generic sound and Bark
?>
In this modified example:
- The
Dogclass still extendsAnimal. - The
makeSound()method in theDogclass calls the parent’smakeSound()method usingparent::makeSound()and appends " and Bark" to the result.
Access Modifiers
Access modifiers determine the visibility of properties and methods in a class. PHP supports public, protected, and private.
Example with Access Modifiers
<?php
class Animal {
public $name;
protected $type;
public function __construct($name, $type) {
$this->name = $name;
$this->type = $type;
}
protected function getType() {
return $this->type;
}
public function makeSound() {
return "Some generic sound";
}
}
class Dog extends Animal {
public function __construct($name) {
parent::__construct($name, "Dog");
}
public function getTypeInfo() {
return $this->getType(); // Accessing protected method
}
public function makeSound() {
return "Bark";
}
}
$dog = new Dog("Buddy");
echo $dog->name; // Outputs: Buddy
echo $dog->getTypeInfo(); // Outputs: Dog
echo $dog->makeSound(); // Outputs: Bark
?>
In this example:
- The
Animalclass has aprotectedproperty$typeand aprotectedmethodgetType(). - The
Dogclass extendsAnimaland can access theprotectedmembers ofAnimalusing its own public methodgetTypeInfo().
Final Classes and Methods
The final keyword prevents classes or methods from being extended or overridden.
Example with final
<?php
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
final public function makeSound() {
return "Some generic sound";
}
}
class Dog extends Animal {
// This will cause an error
// public function makeSound() {
// return "Bark";
// }
}
$dog = new Dog("Buddy");
echo $dog->makeSound(); // Outputs: Some generic sound
?>
In this example:
- The
makeSound()method in theAnimalclass is declared asfinal, meaning it cannot be overridden in any subclass.
These examples illustrate the basics of inheritance in PHP, including how to use the parent keyword, access modifiers, and the final keyword to control inheritance behavior.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.
