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 method in PHP is a method declared within an abstract class or a trait that defines a method signature but provides no implementation. It serves as a strict architectural contract, mandating that any non-abstract (concrete) child class or trait-consuming class must explicitly define the method’s body.

Syntax

An abstract method is defined using the abstract keyword. Because it contains no logic, the declaration is terminated with a semicolon (;) rather than enclosed in curly braces ({}).
abstract class AbstractParent {
    abstract public function executeTask(string $input): bool;
    
    // Abstract methods can be static
    abstract public static function createInstance(): self;
}

trait Enumerable {
    // Traits can declare private abstract methods (PHP 8.0+)
    abstract private function getInternalId(): string;
}

Core Mechanics and Rules

1. Enclosure Constraints An abstract method must be declared inside an abstract class or a trait. Attempting to declare an abstract method inside a standard concrete class will result in a fatal compile-time error. 2. Visibility Constraints Abstract methods within abstract classes cannot be declared as private because they must be accessible to child classes for implementation. However, as of PHP 8.0, traits support abstract private methods. When a class implements an abstract method, the visibility must be the exact same or less restricted. For example:
  • An abstract method declared as protected can be implemented as protected or public.
  • An abstract private method from a trait can be implemented as private, protected, or public.
3. Static Modifier Abstract methods can be declared as static. This is a critical architectural feature used to enforce static method signatures on child classes. The implementing class must also declare the method as static. 4. Signature Compatibility and Variance The implementing method must strictly adhere to the parent’s method signature structure. PHP supports type variance, meaning a valid implementation requires:
  • Exact method name.
  • Matching number of required parameters.
  • Contravariant Parameters: Parameter types can be the exact match or wider (less specific) than the abstract declaration.
  • Covariant Returns: Return types can be the exact match or narrower (more specific) than the abstract declaration.
5. Parameter Expansion While the implementing class must fulfill the required parameters defined by the abstract method, it is permitted to add new parameters to the implementation, provided those new parameters are assigned default values (making them optional).

Implementation Example

The following example demonstrates the enforcement of visibility widening, type variance (covariance and contravariance), and parameter expansion rules during implementation:
abstract class AbstractBase {
    // Declared as protected, requires an array, returns an iterable
    abstract protected function process(array $data): iterable;
}

class ConcreteChild extends AbstractBase {
    // Valid implementation:
    // 1. Visibility widened from protected to public.
    // 2. Parameter type widened from array to iterable (Contravariance).
    // 3. Return type narrowed from iterable to array (Covariance).
    // 4. A new optional parameter ($strict) is legally introduced.
    public function process(iterable $data, bool $strict = false): array {
        $processed = [];
        
        foreach ($data as $item) {
            $processed[] = $strict ? strtoupper($item) : $item;
        }
        
        return $processed;
    }
}
If ConcreteChild failed to implement the process method, omitted the static keyword on an abstract static method, or altered the required parameter types or return type in a way that violated PHP’s covariance and contravariance rules, PHP would throw a fatal error at compile time.
Master PHP with Deep Grasping Methodology!Learn More