PHP OOP Inheritance


Inheritance is one of the fundamental principles of Object-Oriented Programming (OOP) in PHP, allowing a class (called the child or subclass) to inherit properties and methods from another class (called the parent or superclass). This mechanism promotes code reusability and establishes a hierarchical relationship between classes.

Key Concepts of Inheritance

  1. Parent Class: The class whose properties and methods are inherited.
  2. Child Class: The class that inherits from the parent class.
  3. Method Overriding: The child class can provide a specific implementation of a method that is already defined in the parent class.
  4. Access Modifiers: The visibility of inherited properties and methods is affected by the access modifiers used in the parent class (public, protected, private).

Defining Inheritance

In PHP, you can create a child class that extends a parent class using the extends keyword.

Example of Inheritance

// Parent Class class Animal { public $name; public function __construct($name) { $this->name = $name; } public function speak() { return "Animal makes a sound."; } } // Child Class class Dog extends Animal { public function speak() { return "Woof! My name is " . $this->name . "."; } } // Creating an instance of the child class $dog = new Dog("Buddy"); echo $dog->speak(); // Output: Woof! My name is Buddy.


Explanation of the Example:

  • Animal Class: This is the parent class that has a property $name and a method speak().
  • Dog Class: This is the child class that extends the Animal class. It overrides the speak() method to provide a specific implementation for dogs.
  • When the Dog class is instantiated, it can access the name property and also use its own version of the speak() method.

Access Modifiers and Inheritance

  • Public: Public properties and methods can be accessed from anywhere.
  • Protected: Protected properties and methods can be accessed within the class itself and by derived classes, but not from outside the class.
  • Private: Private properties and methods can only be accessed within the class where they are defined and cannot be inherited.

Example with Access Modifiers

class ParentClass { public $publicProperty = "I am public."; protected $protectedProperty = "I am protected."; private $privateProperty = "I am private."; public function getPrivateProperty() { return $this->privateProperty; } } class ChildClass extends ParentClass { public function showProperties() { echo $this->publicProperty; // Accessible echo $this->protectedProperty; // Accessible // echo $this->privateProperty; // Not accessible, would cause an error echo $this->getPrivateProperty(); // Accessible through public method } } $child = new ChildClass(); $child->showProperties(); // Output: // I am public. // I am protected. // I am private.

Advantages of Inheritance

  1. Code Reusability: You can reuse properties and methods from existing classes, reducing redundancy and promoting cleaner code.
  2. Logical Organization: Inheritance helps organize classes into a hierarchy, which can make understanding and maintaining the code easier.
  3. Method Overriding: Child classes can provide specific implementations of methods defined in the parent class, allowing for flexibility and customization.

Abstract Classes and Inheritance

  • An abstract class is a class that cannot be instantiated and is meant to be inherited by other classes. It can contain abstract methods (methods without implementation) that must be defined in child classes.

Example of Abstract Classes

abstract class Shape { abstract public function area(); // Abstract method } class Rectangle extends Shape { private $width; private $height; public function __construct($width, $height) { $this->width = $width; $this->height = $height; } public function area() { return $this->width * $this->height; // Implementation of abstract method } } $rectangle = new Rectangle(5, 10); echo $rectangle->area(); // Output: 50

Interfaces and Inheritance

  • An interface defines a contract that classes can implement. Unlike classes, interfaces cannot contain any properties or implemented methods. A class can implement multiple interfaces, providing a way to achieve multiple inheritance-like behavior

Example of Interfaces

interface Drawable { public function draw(); // Method without implementation } class Circle implements Drawable { public function draw() { echo "Drawing a circle.<br>"; } } class Square implements Drawable { public function draw() { echo "Drawing a square.<br>"; } } $circle = new Circle(); $square = new Square(); $circle->draw(); // Output: Drawing a circle. $square->draw(); // Output: Drawing a square.


Summary

  • Inheritance allows a child class to inherit properties and methods from a parent class, promoting code reuse and logical organization.
  • The extends keyword is used to create a child class that inherits from a parent class.
  • Access modifiers (public, protected, private) control the visibility of inherited properties and methods.
  • Abstract classes cannot be instantiated and can define abstract methods that must be implemented in derived classes.
  • Interfaces provide a way for classes to implement methods defined by the interface, enabling a form of multiple inheritance.

Inheritance is a powerful feature in OOP that helps structure code more effectively, allowing for easier maintenance and scalability.


Types of Inheritance


In PHP object-oriented programming (OOP), inheritance allows a class to inherit properties and methods from another class. There are different levels of inheritance to understand:

1. Single Level Inheritance

  • A class inherits directly from a single parent class.
  • Example:
    class ParentClass { public function sayHello() { echo "Hello from Parent!"; } } class ChildClass extends ParentClass { // Inherits sayHello method } $child = new ChildClass(); $child->sayHello(); // Output: Hello from Parent!

2. Multi-Level Inheritance

  • A class can inherit from another class, which itself is derived from a parent class. This creates a chain of inheritance.
  • Example:
    class GrandparentClass { public function sayHello() { echo "Hello from Grandparent!"; } } class ParentClass extends GrandparentClass { // Inherits sayHello from GrandparentClass } class ChildClass extends ParentClass { // Inherits sayHello from GrandparentClass through ParentClass } $child = new ChildClass(); $child->sayHello(); // Output: Hello from Grandparent!

3. Hierarchical Inheritance

  • Multiple classes inherit from the same parent class.
  • Example:
    class ParentClass { public function greet() { echo "Greetings from Parent!"; } } class ChildClass1 extends ParentClass { // Inherits greet method } class ChildClass2 extends ParentClass { // Inherits greet method } $child1 = new ChildClass1(); $child1->greet(); // Output: Greetings from Parent! $child2 = new ChildClass2(); $child2->greet(); // Output: Greetings from Parent!

4. PHP does not support Multiple Inheritance directly

  • PHP doesn’t allow a class to inherit directly from more than one parent class. To simulate multiple inheritance, PHP uses traits.
  • Example:
    trait TraitA { public function methodA() { echo "Method A from TraitA"; } } trait TraitB { public function methodB() { echo "Method B from TraitB"; } } class ChildClass { use TraitA, TraitB; } $child = new ChildClass(); $child->methodA(); // Output: Method A from TraitA $child->methodB(); // Output: Method B from TraitB

Summary

  • Single Level Inheritance: One parent, one child.
  • Multi-Level Inheritance: Parent -> Child -> Grandchild.
  • Hierarchical Inheritance: Multiple children from one parent.
  • Multiple Inheritance: Not directly supported; use traits instead.

Each level of inheritance helps create a more organized and reusable codebase, allowing child classes to inherit properties and methods from their ancestors.