PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Inheritance and polymorphism are two fundamental concepts in object-oriented programming (OOP) that allow for more flexible and reusable code. In PHP, these concepts are implemented in specific ways. Let’s explore both in detail:

Inheritance

Inheritance allows a class to inherit properties and methods from another class. The class that is inherited from is called the parent class (or base class or superclass), and the class that inherits is called the child class (or derived class or subclass). Inheritance promotes code reuse and establishes a natural hierarchy between classes.

Basic Inheritance

Here is a simple example:

				
					<?php
class Animal {
    public $name;

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

    public function speak() {
        echo "Animal sound";
    }
}

class Dog extends Animal {
    public function speak() {
        echo "Bark";
    }
}

$dog = new Dog("Buddy");
$dog->speak(); // Outputs: Bark
?>

				
			

In this example:

  • The Animal class is the parent class with a property $name and a method speak().
  • The Dog class is the child class that extends Animal and overrides the speak() method to provide its own implementation.

Accessing Parent Class Methods

A child class can also access methods from its parent class using the ‘parent::‘ keyword.

				
					<?php
class Animal {
    public $name;

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

    public function speak() {
        echo "Animal sound";
    }
}

class Dog extends Animal {
    public function speak() {
        parent::speak(); // Call the parent class method
        echo " and Bark";
    }
}

$dog = new Dog("Buddy");
$dog->speak(); // Outputs: Animal sound and Bark
?>

				
			

In this example, ‘Dog::speak()‘ calls the ‘speak()‘ method from the ‘Animal‘ class and then adds its own behavior.

Protected Members

Protected members can be accessed within the class itself, by inheriting classes, but not from outside the class

				
					<?php
class Animal {
    protected $name;

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

    protected function getName() {
        return $this->name;
    }
}

class Dog extends Animal {
    public function speak() {
        echo "My name is " . $this->getName() . " and I Bark";
    }
}

$dog = new Dog("Buddy");
$dog->speak(); // Outputs: My name is Buddy and I Bark
?>

				
			

In this example, ‘Dog::speak()‘ calls the ‘speak()‘ method from the ‘Animal‘ class and then adds its own behavior.

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It is typically implemented using method overriding and interfaces.

Method Overriding

Method overriding occurs when a child class provides a specific implementation of a method that is already defined in its parent class.

				
					<?php
class Animal {
    public function speak() {
        echo "Animal sound";
    }
}

class Dog extends Animal {
    public function speak() {
        echo "Bark";
    }
}

class Cat extends Animal {
    public function speak() {
        echo "Meow";
    }
}

function makeAnimalSpeak(Animal $animal) {
    $animal->speak();
}

$dog = new Dog();
$cat = new Cat();

makeAnimalSpeak($dog); // Outputs: Bark
makeAnimalSpeak($cat); // Outputs: Meow
?>

				
			

Interfaces

Interfaces allow you to define a contract that multiple classes can implement. This is a powerful way to enforce that certain methods are implemented by a class.

				
					<?php
interface Speakable {
    public function speak();
}

class Dog implements Speakable {
    public function speak() {
        echo "Bark";
    }
}

class Cat implements Speakable {
    public function speak() {
        echo "Meow";
    }
}

function makeSpeak(Speakable $animal) {
    $animal->speak();
}

$dog = new Dog();
$cat = new Cat();

makeSpeak($dog); // Outputs: Bark
makeSpeak($cat); // Outputs: Meow
?>

				
			

In this example:

  • The Speakable interface defines a speak() method.
  • Both Dog and Cat classes implement the Speakable interface and provide their own implementation of the speak() method.
  • The makeSpeak() function can accept any object that implements the Speakable interface, demonstrating polymorphism.
  • Inheritance allows a class to inherit properties and methods from another class, promoting code reuse.
  • Polymorphism allows objects of different classes to be treated as objects of a common superclass, typically using method overriding and interfaces.

These concepts help in designing flexible, maintainable, and scalable object-oriented systems in PHP.

Scroll to Top