HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

PHP Abstract Classes

An **Abstract Class** is a class that cannot be instantiated on its own. It serves as a blueprint for other classes. Abstract classes are used when you want to provide a common base for several child classes while ensuring they all implement certain methods.


What is an Abstract Class?

An abstract class is defined using the abstract keyword. It can contain both regular methods (with code) and **Abstract Methods** (without code).

When a child class inherits from an abstract class, it **must** provide the implementation for all the abstract methods defined in the parent class.

Key Rule: If a class contains at least one abstract method, the class itself must also be declared as abstract.

Abstract Methods

An abstract method is a method that is declared in the parent class but has no body (no code). It simply defines the method signature (name and arguments).

<?php
    abstract class Car {
        public $name;
        public function __construct($name) {
            $this->name = $name;
        }
        // Abstract method
        abstract public function intro() : string;
    }
?>

Implementing Abstract Classes

When you extend an abstract class, the child class must implement all abstract methods with the same (or less restricted) visibility.

<?php
    abstract class Animal {
        abstract protected function makeSound();
    }

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

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

    $cat = new Cat();
    $cat->makeSound();

    $dog = new Dog();
    $dog->makeSound();
?>
Note: You cannot create an object from an abstract class. For example, $myAnimal = new Animal(); would result in a Fatal Error.

Abstract Class Rules

When inheriting from an abstract class, there are three main rules the child class must follow:

  • The child class must implement all abstract methods of the parent.
  • The child class's methods must have the same (or lower) visibility (e.g., if the parent method is protected, the child's can be protected or public, but not private).
  • The number and type of required arguments must match the parent's definition.

Why Use Abstract Classes?

  • Code Standardization: Force child classes to follow a specific structure.
  • Code Reusability: Provide shared functionality (regular methods) while allowing customization (abstract methods).
  • Security & Robustness: Prevent direct instantiation of generic base classes that aren't meant to be used on their own (like a generic Database class).
  • Clear Architecture: Help developers understand exactly what a child class is expected to do.
Tip: Abstract classes are great for creating a "partially finished" class that others can complete. If you need a "completely empty" template, consider using **Interfaces**.

Summary

  • abstract keyword is used for both classes and methods.
  • Abstract classes cannot be instantiated.
  • Abstract methods have no body.
  • Child classes must implement all abstract methods.
  • Abstract classes can also contain regular (non-abstract) properties and methods.