WikiGalaxy

Personalize

PHP Abstract Classes

Introduction to Abstract Classes:

Abstract classes in PHP are declared with the <abstract> keyword. They cannot be instantiated directly and are designed to be inherited by other classes.

Purpose of Abstract Classes:

Abstract classes provide a blueprint for other classes. They can include abstract methods, which must be defined in the child classes.


abstract class Animal {
  abstract public function makeSound();
  public function sleep() {
    echo "Sleeping...";
  }
}
    

Defining Abstract Methods:

Abstract methods are declared without a body and must be implemented by subclasses. They define a contract that all derived classes must follow.

Use Case:

Consider an abstract class Animal with an abstract method makeSound(). Any subclass must implement this method.

Console Output:

Sleeping...

Implementing Abstract Methods

Concrete Implementation:

When a class extends an abstract class, it must implement all abstract methods. This ensures consistency across subclasses.


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

Example Explanation:

Here, the Dog class extends the Animal abstract class and provides an implementation for the makeSound() method.

Console Output:

Woof!

Abstract Class with Properties

Using Properties:

Abstract classes can also have properties, which can be used by subclasses to store data relevant to the class.


abstract class Vehicle {
  protected $speed;
  abstract public function accelerate();
}
    

Explanation:

The Vehicle abstract class has a protected property $speed, which is accessible to its subclasses.

Console Output:

No direct output, property demonstration.

Abstract Class with Constructor

Constructor in Abstract Classes:

Abstract classes can have constructors, which can be used to initialize properties that are common to all subclasses.


abstract class Device {
  protected $brand;
  public function __construct($brand) {
    $this->brand = $brand;
  }
  abstract public function operate();
}
    

Explanation:

The constructor in the Device class initializes the $brand property, which is accessible to subclasses.

Console Output:

No direct output, constructor demonstration.

Multiple Abstract Methods

Defining Multiple Methods:

An abstract class can contain multiple abstract methods, each of which must be implemented by subclasses.


abstract class Shape {
  abstract public function area();
  abstract public function perimeter();
}
    

Explanation:

The Shape abstract class defines two abstract methods, area() and perimeter(), which must be implemented by any subclass.

Console Output:

No direct output, method declaration.

Final Methods in Abstract Classes

Using Final Methods:

Abstract classes can have final methods, which cannot be overridden by subclasses, ensuring consistent behavior.


abstract class Appliance {
  final public function plugIn() {
    echo "Appliance plugged in.";
  }
  abstract public function use();
}
    

Explanation:

The plugIn() method in the Appliance class is final, meaning it cannot be changed by subclasses.

Console Output:

Appliance plugged in.

Inheritance from Abstract Classes

Extending Abstract Classes:

When a class inherits from an abstract class, it gains access to its implemented methods and must implement its abstract methods.


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

Explanation:

The Cat class inherits from the Animal abstract class and implements the makeSound() method.

Console Output:

Meow!

Abstract Classes vs Interfaces

Key Differences:

Abstract classes can have both implemented and abstract methods, while interfaces can only have abstract methods. Abstract classes can also have properties, whereas interfaces cannot.


// Abstract class example
abstract class Machine {
  abstract public function start();
  public function stop() {
    echo "Machine stopped.";
  }
}

// Interface example
interface Operatable {
  public function operate();
}
    

Explanation:

The Machine class is an abstract class with both an abstract and a concrete method, while Operatable is an interface with only abstract methods.

Console Output:

Machine stopped.

Real-world Example: Payment System

Abstract Class in Action:

A payment system can use an abstract class to define a template for different payment methods, ensuring each method implements necessary operations.


abstract class Payment {
  abstract public function processPayment($amount);
  public function validate() {
    echo "Payment validated.";
  }
}

class CreditCardPayment extends Payment {
  public function processPayment($amount) {
    echo "Processing credit card payment of $amount.";
  }
}
    

Explanation:

The Payment abstract class defines a structure for payment processing, while CreditCardPayment implements the specific logic for credit card transactions.

Console Output:

Payment validated. Processing credit card payment of $amount.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025