{"id":22367207,"url":"https://github.com/sukhoy94/php-interfaces","last_synced_at":"2025-07-08T19:11:07.729Z","repository":{"id":117094722,"uuid":"609509088","full_name":"sukhoy94/php-interfaces","owner":"sukhoy94","description":"PHP interfaces explained","archived":false,"fork":false,"pushed_at":"2023-03-04T14:05:08.000Z","size":3,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T15:54:40.775Z","etag":null,"topics":["interfaces","oop","php","php-interface"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sukhoy94.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-04T11:51:36.000Z","updated_at":"2023-03-17T14:53:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"4304350d-e939-445c-86aa-a0292ab5f8eb","html_url":"https://github.com/sukhoy94/php-interfaces","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sukhoy94/php-interfaces","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fphp-interfaces","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fphp-interfaces/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fphp-interfaces/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fphp-interfaces/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sukhoy94","download_url":"https://codeload.github.com/sukhoy94/php-interfaces/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukhoy94%2Fphp-interfaces/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264331722,"owners_count":23591963,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["interfaces","oop","php","php-interface"],"created_at":"2024-12-04T18:16:47.084Z","updated_at":"2025-07-08T19:11:07.708Z","avatar_url":"https://github.com/sukhoy94.png","language":"PHP","readme":"# Interfaces in PHP\n\nObject interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are implemented. Interfaces share a namespace with classes and traits, so they may not use the same name.\n\n\n# What can you have inside an interface ? \n\n- Obviously, public methods without implementation: immediately after the header (signature) of the method, you should end it with a semicolon:\n\n```\ninterface MyInterface\n{\n    public function firstFunction();\n    public static function secondFunction(SomeType $variable);\n}\n```\n\n\n- A little less obvious (although it is described in the manual) is the fact that an interface can contain constants (of course, only public ones!):\n\n```\ninterface SomeInterface\n{\n    public const STATUSES = [\n        'OK'    =\u003e 0,\n        'ERROR' =\u003e 1,\n    ];\n}\n\nif (SomeInterface::STATUSES['OK'] === $status) {\n    // ...\n}\n```\n\n# What can't an interface contain?\n\nNothing else can. Except for public methods headers and public constants.\nCannot be included in the interface:\n- Any properties\n- Non-Public Methods\n- Methods with implementation\n- Non-public constants\n\n# Signature compatibility\n\nA signature is a description of a function (method), which includes:\n- Access modifier\n- Function (method) name\n- Argument list, where for each argument: Type, Name, Default value or the \"three dots\" operator\n- return type\n\n\nSuppose we have two functions, A and B.\nThe signature of a function B is considered compatible with A (order matters, relation is not symmetrical!) in the strict sense if:\n\n#### 1. They match perfectly\n\nA trivial case, there is nothing to comment on here.\n\n#### 2. B adds default arguments to A\n\nA:\n```\nfunction foo($x);\n```\n\nCompatible B:\n```\nfunction foo($x, $y = null);\nfunction foo($x, ...$args);\n```\n\n\n#### B narrows the range of A\n\nA:\n\n```\nfunction foo(int $x);\n```\n\nCompatible B:\n```\n// A allowed to return any values, in B this area is narrowed only to integers\nfunction foo(int $x): int;\n```\n\n\n# Interface inheritance\n\nInterfaces can inherit from each other:\n\n```\ninterface SpeakableInterface\n{\n    public function speak(): void;\n}\n\ninterface HelloInterface extends SpeakableInterface\n{\n    public function hello();\n}\n\nclass User implements HelloInterface\n{    \n    public function hello()\n    {\n        echo 'Hello';\n    }\n    \n    public function speak(): void\n    {\n        echo 'Speak Hello';\n    }\n}\n```\n\nClass which implements HelloInterface must have implements all methods from HelloInterface and from SpeakableInterface.\n\nInteresting fact that there are multiple inheritance in case of interfaces:\n\n```\ninterface SpeakableInterface\n{\n    public function speak(): void;\n}\n\ninterface WritebleInterface\n{\n    public function write(): void;\n}\n\ninterface HelloInterface \n    extends SpeakableInterface, WritebleInterface\n{\n    public function hello();\n}\n\nclass User implements HelloInterface\n{    \n    public function hello()\n    {\n        echo 'Hello';\n    }\n    \n    public function speak(): void\n    {\n        echo 'Speak Hello';\n    }\n    \n    public function write(): void\n    {\n        echo 'Write Hello';\n    }\n}\n```\n\nThe class implementing the interface must declare all methods in the interface with a compatible signature. A class can implement multiple interfaces which declare a method with the same name. In this case, the implementation must follow the signature compatibility rules for all the interfaces. So covariance and contravariance can be applied.\n\n\nIn a derived interface, you can override a method from the parent interface. But only on the condition that either its signature will exactly match the signature of the parent, or it will be compatible (see the previous section):\n\n```\ninterface First\n{\n    public function foo(int $x);\n}\n\ninterface Second extends First\n{\n\n    // It's possible, but pointless\n    public function foo(int $x);\n    \n    // This is not allowed, fatal error Declaration must be compatible\n    public function foo(int $x, int $y);\n    \n    // This is possible because this signature is compatible with the parent - we just added an optional argument\n    public function foo(int $x, int $y = 0);\n    \n    // This is also possible, all arguments after \"...\" are optional\n    public function foo(int $x, ...$args);\n    \n    // And this is also possible\n    public function foo(int $x, ...$args): int;\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsukhoy94%2Fphp-interfaces","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsukhoy94%2Fphp-interfaces","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsukhoy94%2Fphp-interfaces/lists"}