> ## 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.

# PHP Interface

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.

```php theme={"dark"}
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.

```php theme={"dark"}
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.

```php theme={"dark"}
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.

```php theme={"dark"}
function processLog(LoggerInterface $logger, string $message): void {
    $logger->log($message);
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor PHP Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
