{
    "slug": "interfaces",
    "term": "Interfaces",
    "category": "php",
    "difficulty": "intermediate",
    "short": "Contracts that define a set of method signatures a class must implement, enabling polymorphism without inheritance.",
    "long": "PHP interfaces define public method signatures without implementation. A class that implements an interface guarantees it provides those methods, enabling code to depend on the interface contract rather than a concrete implementation — a key principle of Dependency Inversion. Interfaces support multiple implementation (unlike single-class inheritance), can extend other interfaces, and since PHP 8.0 can declare constants. Type-hinting against interfaces rather than concrete classes is the basis of testable, loosely coupled PHP architecture.",
    "aliases": [
        "PHP interfaces",
        "interface keyword",
        "PHP contracts"
    ],
    "tags": [
        "php",
        "oop",
        "solid",
        "principles"
    ],
    "misconception": "Interfaces with only one or two methods are too small to be worth defining. Single-method interfaces (like Stringable, Countable) are some of the most powerful — they enable type-safe duck typing and allow unrelated classes to participate in the same behaviour.",
    "why_it_matters": "Interfaces define contracts — they let you depend on behaviour rather than implementation, making code testable and swappable. Without interfaces, every dependency is a concrete class and every test requires the real thing.",
    "common_mistakes": [
        "Creating interfaces for every class regardless of whether multiple implementations exist or are planned.",
        "Putting too many methods in one interface — violates Interface Segregation, forces implementors to stub unused methods.",
        "Naming interfaces with I prefix (IMailer) — PHP convention is to use the role name (Mailer, MailerInterface).",
        "Duplicating the concrete class method signatures exactly — interfaces should model client needs, not implementation details."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "abstract_classes",
        "dependency_injection",
        "interface_segregation",
        "open_closed_principle"
    ],
    "prerequisites": [
        "dependency_injection",
        "dependency_inversion",
        "type_declarations"
    ],
    "refs": [
        "https://www.php.net/manual/en/language.oop5.interfaces.php"
    ],
    "bad_code": "// Concrete type dependency — cannot swap implementations:\nclass OrderService {\n    public function __construct(private MySQLOrderRepository $repo) {}\n}\n\n// Interface-based — swappable:\ninterface OrderRepositoryInterface {\n    public function find(int $id): Order;\n    public function save(Order $o): void;\n}\nclass OrderService {\n    public function __construct(private OrderRepositoryInterface $repo) {}\n}",
    "good_code": "// Interfaces define contracts — no implementation\ninterface Mailer {\n    public function send(Message $message): void;\n    public function sendBulk(array $messages): int;\n}\n\ninterface Loggable {\n    public function toLog(): array;\n}\n\n// A class can implement multiple interfaces\nclass SmtpMailer implements Mailer {\n    public function send(Message $message): void {\n        // SMTP implementation\n    }\n    public function sendBulk(array $messages): int {\n        foreach ($messages as $m) { $this->send($m); }\n        return count($messages);\n    }\n}\n\n// Type hint the interface — never the concrete class\nclass NotificationService {\n    public function __construct(private Mailer $mailer) {} // swappable\n}",
    "quick_fix": "Define an interface for any dependency that might have multiple implementations or needs to be mocked in tests — inject the interface not the concrete class",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/interfaces",
        "html_url": "https://codeclaritylab.com/glossary/interfaces",
        "json_url": "https://codeclaritylab.com/glossary/interfaces.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Interfaces](https://codeclaritylab.com/glossary/interfaces) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/interfaces"
            }
        }
    }
}