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.
At a glance
- Purpose
- Server-side web applications
- File extension
- .php
- Runs in
- PHP runtime on a server or CLI
- Usually used with
- HTML, databases and web servers