Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class to inherit properties and methods from another class. This promotes code reusability and establishes a natural hierarchy between classes.

Key Concepts

  1. Parent Class (Base Class or Superclass): The class whose properties and methods are inherited.
  2. Child Class (Derived Class or Subclass): The class that inherits properties and methods from the parent class.
  3. extends Keyword: Used to create a child class that inherits from a parent class.

Example

Let's start with a simple example to illustrate inheritance in PHP.

Parent Class

<?php
class Animal {
    public $name;
    public $color;

    public function __construct($name, $color) {
        $this->name = $name;
        $this->color = $color;
    }

    public function describe() {
        return "This is a $this->color $this->name.";
    }
}
?>

Child Class

<?php
class Dog extends Animal {
    public function bark() {
        return "Woof! Woof!";
    }
}

$dog = new Dog("Dog", "brown");
echo $dog->describe(); // Output: This is a brown Dog.
echo $dog->bark();     // Output: Woof! Woof!
?>

Explanation

  • Parent Class (Animal): Defines properties name and color, and a method describe().
  • Child Class (Dog): Inherits properties and methods from Animal and adds a new method bark().

Practical Exercise

Task

  1. Create a parent class Vehicle with properties make and model, and a method getDetails().
  2. Create a child class Car that extends Vehicle and adds a method drive().
  3. Instantiate the Car class and call both getDetails() and drive() methods.

Solution

<?php
class Vehicle {
    public $make;
    public $model;

    public function __construct($make, $model) {
        $this->make = $make;
        $this->model = $model;
    }

    public function getDetails() {
        return "Make: $this->make, Model: $this->model";
    }
}

class Car extends Vehicle {
    public function drive() {
        return "The car is driving.";
    }
}

$car = new Car("Toyota", "Corolla");
echo $car->getDetails(); // Output: Make: Toyota, Model: Corolla
echo $car->drive();      // Output: The car is driving.
?>

Explanation

  • Parent Class (Vehicle): Defines properties make and model, and a method getDetails().
  • Child Class (Car): Inherits properties and methods from Vehicle and adds a new method drive().

Common Mistakes

  1. Forgetting to Call the Parent Constructor: If the parent class has a constructor, you should call it from the child class constructor using parent::__construct().

    class Car extends Vehicle {
        public function __construct($make, $model) {
            parent::__construct($make, $model);
        }
    
        public function drive() {
            return "The car is driving.";
        }
    }
    
  2. Overriding Methods Incorrectly: When overriding a method in the child class, ensure the method signature matches the parent class method.

Summary

  • Inheritance allows a class to inherit properties and methods from another class, promoting code reusability.
  • Use the extends keyword to create a child class that inherits from a parent class.
  • Always call the parent constructor if the parent class has one.
  • Be cautious when overriding methods to ensure the method signatures match.

In the next topic, we will explore Interfaces and Abstract Classes, which provide additional ways to define and enforce the structure of your classes.

PHP Programming Course

Module 1: Introduction to PHP

Module 2: Control Structures

Module 3: Functions

Module 4: Arrays

Module 5: Working with Forms

Module 6: Working with Files

Module 7: Object-Oriented Programming (OOP)

Module 8: Working with Databases

Module 9: Advanced PHP Techniques

Module 10: PHP Frameworks and Best Practices

Module 11: Project: Building a Web Application

© Copyright 2024. All rights reserved