Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Inheritance in PHP

Introduction

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit the properties and methods of another class. It promotes code reusability and a hierarchical class structure. In PHP, inheritance is achieved using the extends keyword.

Basic Inheritance

To demonstrate basic inheritance, we will create two classes: a parent class Animal and a child class Dog that inherits from Animal.

<?php
class Animal {
    public $name;
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    public function eat() {
        echo $this->name . " is eating.\n";
    }
}

class Dog extends Animal {
    public function bark() {
        echo $this->name . " is barking.\n";
    }
}

$dog = new Dog("Buddy");
$dog->eat();
$dog->bark();
?>
                
Buddy is eating.
Buddy is barking.

In this example, the Dog class inherits the name property and eat method from the Animal class and adds a new method bark.

Overriding Methods

Child classes can override methods of their parent class. This allows the child class to provide a specific implementation for a method that is already defined in the parent class.

<?php
class Animal {
    public $name;
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    public function eat() {
        echo $this->name . " is eating.\n";
    }
}

class Dog extends Animal {
    public function eat() {
        echo $this->name . " is eating dog food.\n";
    }
}

$dog = new Dog("Buddy");
$dog->eat(); // Outputs: Buddy is eating dog food.
?>
                
Buddy is eating dog food.

In this example, the Dog class overrides the eat method of the Animal class to provide a specific implementation.

Using the Parent's Constructor

When a child class overrides the constructor, it can still call the parent's constructor using the parent::__construct method.

<?php
class Animal {
    public $name;
    
    public function __construct($name) {
        $this->name = $name;
    }
}

class Dog extends Animal {
    public $breed;
    
    public function __construct($name, $breed) {
        parent::__construct($name);
        $this->breed = $breed;
    }
    
    public function display() {
        echo $this->name . " is a " . $this->breed . ".\n";
    }
}

$dog = new Dog("Buddy", "Golden Retriever");
$dog->display(); // Outputs: Buddy is a Golden Retriever.
?>
                
Buddy is a Golden Retriever.

In this example, the Dog class's constructor calls the Animal class's constructor using parent::__construct($name).

Access Control

PHP provides three levels of access control for class members: public, protected, and private.

  • public: Accessible from anywhere.
  • protected: Accessible within the class itself and by inheriting classes.
  • private: Accessible only within the class itself.
<?php
class Animal {
    public $name;
    protected $type;
    private $age;
    
    public function __construct($name, $type, $age) {
        $this->name = $name;
        $this->type = $type;
        $this->age = $age;
    }
    
    public function display() {
        echo $this->name . " is a " . $this->type . " and is " . $this->age . " years old.\n";
    }
}

class Dog extends Animal {
    public function getType() {
        return $this->type; // Accessible because it's protected
    }
}

$dog = new Dog("Buddy", "Dog", 5);
$dog->display();
echo $dog->getType(); // Outputs: Dog
?>
                
Buddy is a Dog and is 5 years old.
Dog

In this example, the $type property is protected, so it is accessible within the Dog class, while the $age property is private, so it is only accessible within the Animal class.

Final Keyword

The final keyword prevents a class from being inherited or a method from being overridden.

<?php
final class Animal {
    public $name;
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    final public function display() {
        echo $this->name . " is an animal.\n";
    }
}

// This will cause an error
class Dog extends Animal {
    // Cannot override final method
    public function display() {
        echo $this->name . " is a dog.\n";
    }
}
?>
                

In this example, attempting to inherit from the Animal class or override the display method will result in an error because they are declared as final.