Syntax
interface Greets { public function greet(): string; }
class Greeter implements Greets { /* ... */ }Examples
Implementing an interface
A small, runnable example of this syntax.
<?php
interface Greets {
public function greet(): string;
}
class Greeter implements Greets {
public function __construct(private string $name) {}
public function greet(): string { return "Hello, {$this->name}!"; }
}
echo (new Greeter("Ada"))->greet();
?>Best practices
- Keep public interfaces small and focused so implementations remain easy to understand and test.
- Keep examples small while learning, then combine the idea with a real project.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual