PHP OOP Abstract class


An abstract class in PHP is a class that cannot be instantiated on its own and is meant to serve as a base for other classes. Abstract classes are used to provide a common interface and functionality that can be inherited by child classes, while still requiring those child classes to implement certain specific methods.

Abstract classes are particularly useful when you have a base class that includes general code, but you want to enforce that child classes must provide concrete implementations of certain behaviors.

Key Features of Abstract Classes

  1. Cannot Be Instantiated: Abstract classes cannot be instantiated directly; they are meant to be inherited by other classes.
  2. Abstract Methods: An abstract class can have abstract methods, which are methods that are declared but not defined in the abstract class. These methods must be implemented in any subclass.
  3. Can Have Concrete Methods and Properties: Abstract classes can also have fully defined methods and properties that can be inherited by the subclasses.

Declaring an Abstract Class

To define an abstract class in PHP, use the abstract keyword before the class declaration. Similarly, abstract methods are declared using the abstract keyword within an abstract class.

Example

abstract class Animal { // Abstract method - must be implemented by subclasses abstract public function makeSound(); // Concrete method - can be inherited by subclasses public function eat() { echo "This animal is eating.\n"; } } // Dog class extends Animal and implements the makeSound method class Dog extends Animal { public function makeSound() { echo "Woof! Woof!\n"; } } // Cat class extends Animal and implements the makeSound method class Cat extends Animal { public function makeSound() { echo "Meow! Meow!\n"; } } // Instantiate objects of Dog and Cat $dog = new Dog(); $cat = new Cat(); $dog->makeSound(); // Outputs: Woof! Woof! $dog->eat(); // Outputs: This animal is eating. $cat->makeSound(); // Outputs: Meow! Meow! $cat->eat(); // Outputs: This animal is eating.

Explanation

  1. Abstract Class (Animal):

    • The Animal class is declared as abstract and cannot be instantiated directly.
    • It has an abstract method makeSound(), which has no body. This method must be implemented by all child classes.
  2. Concrete Method (eat()):

    • The Animal class also has a concrete method eat(), which provides functionality that can be used by all subclasses without modification.
  3. Child Classes (Dog and Cat):

    • Both Dog and Cat classes extend the Animal class and must implement the makeSound() method because it was declared as abstract in the parent class.
    • These classes can also use the eat() method from the Animal class.

Key Points About Abstract Classes

  1. Cannot Instantiate: You cannot create an instance of an abstract class.

    $animal = new Animal(); // Error: Cannot instantiate abstract class
  2. Must Implement Abstract Methods: All subclasses of an abstract class must implement all the abstract methods of the parent class.

    abstract class Vehicle { abstract public function startEngine(); } class Car extends Vehicle { public function startEngine() { echo "Car engine started.\n"; } } $car = new Car(); // Works fine, since startEngine is implemented.
  3. Polymorphism: Abstract classes are often used to enforce polymorphism in your code, allowing for consistent method definitions while enabling different implementations across subclasses.

Practical Use Cases for Abstract Classes

  • Template Design Pattern: Abstract classes are useful in situations where you want to provide a common template for a group of related classes.
  • Base Class with Shared Functionality: Use abstract classes to provide a base class with shared properties and methods, but enforce that specific methods are defined in each subclass.

For instance, you might have a Database abstract class with methods for connecting, selecting, inserting, etc., and specific database types like MySQLDatabase or PostgreSQLDatabase that implement the connection specifics.

Example: Template for Different Payment Methods

Consider a scenario where you have different payment methods, each requiring certain operations to be implemented in their way:

abstract class PaymentMethod { abstract public function pay($amount); public function printReceipt($amount) { echo "Paid amount: $" . $amount . "\n"; } } class CreditCardPayment extends PaymentMethod { public function pay($amount) { echo "Paying $" . $amount . " using Credit Card.\n"; } } class PayPalPayment extends PaymentMethod { public function pay($amount) { echo "Paying $" . $amount . " using PayPal.\n"; } } $payment = new CreditCardPayment(); $payment->pay(100); $payment->printReceipt(100); $paypal = new PayPalPayment(); $paypal->pay(200); $paypal->printReceipt(200);

Summary

  • Abstract Classes provide a common base class that cannot be instantiated and is used to enforce structure on subclasses.
  • Abstract Methods are methods declared in an abstract class that must be implemented by any subclass.
  • Concrete Methods and Properties: Abstract classes can also contain concrete methods and properties that can be inherited by subclasses.
  • Abstract classes are very useful when designing a shared interface or template that all subclasses should follow, while still allowing flexibility in implementation.

Abstract classes are a powerful concept in object-oriented programming, helping enforce a consistent interface across multiple related classes while sharing code and ensuring key functionalities are always implemented by subclasses.