PHP Inheritance – Extend Classes & Reuse Code ๐
Inheritance allows a class (child) to inherit properties and methods from another class (parent). This helps in code reuse and maintaining cleaner code.
In PHP, inheritance is done using the extends keyword.
๐น Basic PHP Inheritance
When a child class inherits from a parent class, it gets access to all public and protected properties and methods.
๐ Example 1: Simple Inheritance
<?php
// Parent class
class Animal {
public $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return "This is a " . $this->name;
}
}
// Child class inheriting from Animal
class Dog extends Animal {
public function bark() {
return "Woof! Woof! ๐ถ";
}
}
// Creating an object of Dog
$myDog = new Dog();
$myDog->setName("Golden Retriever");
echo $myDog->getName(); // โ
Inherited from Animal
echo "<br>";
echo $myDog->bark(); // โ
Method from Dog class
?>
The Dog class inherited the methods from the Animal class. It also has its own method bark().
๐น Overriding Parent Methods
A child class can override a method from the parent class by redefining it.
๐ Example 2: Method Overriding
<?php
class Vehicle {
public function start() {
return "Vehicle is starting... ๐";
}
}
// Child class overrides the start method
class Car extends Vehicle {
public function start() {
return "Car is revving up! ๐";
}
}
$myCar = new Car();
echo $myCar->start(); // Output: Car is revving up!
?>
Here, the Car class overrides the start() method of the Vehicle class.
๐น Using Parent Methods in Child Class
The parent:: keyword lets you call a method from the parent class inside the child class.
๐ Example 3: Calling Parent Methods
<?php
class Laptop {
public function start() {
return "Laptop is booting up... ๐ป";
}
}
class GamingLaptop extends Laptop {
public function start() {
return parent::start() . " Loading games! ๐ฎ";
}
}
$gamerPC = new GamingLaptop();
echo $gamerPC->start(); // โ
Calls parent method + child method
?>
We used parent::start() inside the child class to call the method from the parent class.
๐น Protected Properties & Methods in Inheritance
The protected keyword allows child classes to access properties/methods while keeping them hidden from outside the class.
๐ Example 4: Using Protected Properties
<?php
class Person {
protected $name;
public function setName($name) {
$this->name = $name;
}
}
// Child class can access protected properties
class Employee extends Person {
public function getName() {
return "Employee Name: " . $this->name;
}
}
$emp = new Employee();
$emp->setName("John Doe");
echo $emp->getName(); // โ
Works fine
?>
The $name property is protected, so itโs accessible in the child class but not outside the class.
๐ฏ Key Takeaways
extendsโ Used to inherit a class.- Child classes inherit public & protected properties/methods.
- Child classes can override parent methods.
- Use
parent::method()to call a parent method inside a child class. - Private properties/methods are not inherited.
๐ Practice Time!
Try creating a Superhero class ๐ฆธ that inherits from a Human class and adds special powers!