PHP Inheritance

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

Try It Now

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!
?>

Try It Now

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

Try It Now

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

Try It Now

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!