Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An abstract class in PHP is a restricted class that cannot be instantiated directly and serves exclusively as a base class for other classes. Declared using the abstract keyword, it establishes a strict contractual blueprint by defining abstract methods that derived classes must implement, while optionally providing shared concrete method implementations, properties, and constants.
abstract class AbstractBase {
    // Standard property
    protected string $status = 'initialized';

    // Abstract method: declares signature only, no body allowed
    abstract protected function processPayload(array $data): bool;

    // Concrete method: provides default implementation inherited by children
    public function getStatus(): string {
        return $this->status;
    }
}

class ConcreteImplementation extends AbstractBase {
    // Must implement the abstract method with a compatible signature
    protected function processPayload(array $data): bool {
        if (empty($data)) {
            return false;
        }
        $this->status = 'processed';
        return true;
    }
}

Core Rules and Mechanics

  • Instantiation Prohibition: Attempting to instantiate an abstract class directly (e.g., $obj = new AbstractBase();) will throw a PHP Fatal Error.
  • Abstract Method Declaration: Abstract methods are declared with the abstract keyword and terminated with a semicolon instead of a method body {}.
  • Class Declaration Requirement: If a class contains at least one abstract method, the class itself must be explicitly declared as abstract.
  • Implementation Enforcement: Any non-abstract (concrete) child class extending an abstract parent must implement all abstract methods defined in the parent hierarchy. If the child class does not implement all abstract methods, it must also be declared abstract.
  • Signature Compatibility: The method implementation in the child class must strictly adhere to the parent’s abstract method signature. This requires matching the method name, the number of required parameters, and type declarations (respecting PHP’s covariance and contravariance rules). The child class may add optional parameters (parameters with default values) that are not present in the parent signature.
  • Visibility Constraints: The visibility of the implemented method in the child class must be the same as, or less restricted than, the abstract method in the parent. For example, an abstract method declared as protected can be implemented as protected or public in the child class, but cannot be restricted to private.
  • Constructors: Abstract classes can define __construct() methods. These can be either concrete (to initialize inherited properties) or abstract (to force child classes to implement a specific constructor signature).
Master PHP with Deep Grasping Methodology!Learn More