Skip to content

Instantly share code, notes, and snippets.

@anikwai
Last active December 1, 2023 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anikwai/0c24471829ab47eb0772052d7695f889 to your computer and use it in GitHub Desktop.
Save anikwai/0c24471829ab47eb0772052d7695f889 to your computer and use it in GitHub Desktop.
inheritance_polymorphism

What is the difference between inheritance and polymorphism in PHP?

1.0 Inheritance

Inheritance in PHP allows a class to inherit properties and methods from another class. Here's a simple example:

<?php

// Base class or Parent class
class Vehicle {
    public $make;
    public $model;
    public $color;

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

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

// Derived class or Child class
class Car extends Vehicle {
    public $doorCount;

    public function __construct($make, $model, $color, $doorCount) {
        parent::__construct($make, $model, $color); // Call the parent constructor
        $this->doorCount = $doorCount;
    }

    public function getCarDetails() {
        return $this->getDetails() . ", Doors: $this->doorCount";
    }
}

// Create a new Car object
$myCar = new Car('Toyota', 'Corolla', 'Blue', 4);

// Output the details of the car
echo $myCar->getCarDetails(); // Make: Toyota, Model: Corolla, Color: Blue, Doors: 4

?>

In this example, the Car class inherits from the Vehicle class. This means that Car has access to the properties and methods of Vehicle, such as $make, $model, $color, and getDetails(). The Car class also defines an additional property $doorCount and a method getCarDetails() that extends the functionality of the base class. The parent::__construct() call is used to invoke the constructor of the base class from the derived class's constructor.

2.0 Polymorphism

Inheritance and polymorphism are two fundamental concepts in object-oriented programming (OOP), but they serve different purposes:

Inheritance allows a class to inherit properties and methods from another class, enabling code reuse and the creation of a class hierarchy. Polymorphism allows objects of different classes to be treated as objects of a common superclass. It's the ability of different classes to provide different implementations of the same method, which can be called through the same interface. Here's an example that illustrates both inheritance and polymorphism in PHP:

<?php

// Base class or Parent class
abstract class Animal {
    public function communicate() {
        return "I am an animal and I make a sound.";
    }
}

// Derived class or Child class
class Dog extends Animal {
    public function communicate() {
        return "Bark!";
    }
}

// Another derived class
class Cat extends Animal {
    public function communicate() {
        return "Meow!";
    }
}

// Polymorphism in action
function makeAnimalCommunicate(Animal $animal) {
    echo $animal->communicate() . PHP_EOL;
}

// Create instances of Dog and Cat
$dog = new Dog();
$cat = new Cat();

// Both Dog and Cat are Animals, so they can be passed to the function
makeAnimalCommunicate($dog); // Outputs: Bark!
makeAnimalCommunicate($cat); // Outputs: Meow!

?>

In this example:

The Animal class is an abstract class that defines a method communicate. The Dog and Cat classes inherit from Animal and provide their own implementations of the communicate method. The function makeAnimalCommunicate demonstrates polymorphism. It accepts any object that is an instance of Animal (or a subclass of Animal) and calls the communicate method on it. The actual method that gets called depends on the class of the object passed to the function, even though the function interface is the same for all classes that extend Animal. This shows how inheritance is used to establish a relationship between classes, while polymorphism is used to execute different behaviors through a common interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment