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 interface in PHP is an object-oriented programming construct that defines a strict contract specifying which methods a class must implement, without defining how those methods are implemented. It acts as a purely structural blueprint, enforcing method signatures and polymorphism across disparate classes.

Syntax and Declaration

Interfaces are declared using the interface keyword. Classes adhere to an interface using the implements keyword.
interface LoggerInterface {
    public function log(string $message): void;
}

class FileLogger implements LoggerInterface {
    public function log(string $message): void {
        file_put_contents('app.log', $message);
    }
}

Technical Rules and Constraints

  1. Method Visibility: All methods declared within an interface must have public visibility.
  2. No Implementation: Methods inside an interface cannot contain a body. They consist solely of the method signature.
  3. No Instantiation: Interfaces cannot be instantiated directly. Attempting to call new LoggerInterface() will result in a fatal error.
  4. Properties: Interfaces cannot declare class properties (variables).
  5. Constants: Interfaces can declare constants. Implementing classes inherit these constants, and prior to PHP 8.1, they could not be overridden.
  6. Signature Compatibility: The implementing class must adhere to the interface’s method signatures, subject to PHP’s variance rules:
    • Covariance: The implementing method’s return type can be more specific (narrower) than the interface’s return type.
    • Contravariance: The implementing method’s parameter types can be less specific (wider) than the interface’s parameter types.
    • Parameter Extension: The implementing class may add extra parameters to the method signature, provided that all additional parameters have default values (making them optional).

Multiple Implementations

Unlike class inheritance (where a PHP class can only extend one parent class), a single class can implement multiple interfaces. The interface names are separated by commas.
interface FormatterInterface {
    public function format(array $data): string;
}

class DataHandler implements LoggerInterface, FormatterInterface {
    public function log(string $message): void {
        // Implementation required
    }

    public function format(array $data): string {
        // Implementation required
        return json_encode($data);
    }
}

Interface Inheritance

Interfaces can extend other interfaces using the extends keyword. An interface can extend multiple interfaces simultaneously, combining their method requirements.
interface ReadableInterface {
    public function read(): string;
}

interface WritableInterface {
    public function write(string $data): bool;
}

interface FileSystemInterface extends ReadableInterface, WritableInterface {
    public function getPermissions(): int;
}
If a class implements FileSystemInterface, it is strictly required to provide concrete implementations for read(), write(), and getPermissions().

Type Hinting

Interfaces are heavily utilized in PHP’s type system. You can type-hint an interface in function or method parameters to accept any object whose class implements that interface, regardless of the object’s specific class hierarchy.
function processLog(LoggerInterface $logger, string $message): void {
    $logger->log($message);
}
Master PHP with Deep Grasping Methodology!Learn More