Implementing Interfaces
Define contracts with interface and implement them in multiple classes.
Implementing Interfaces is a free PHP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro: PHP Interfaces
An interface defines a contract: a list of method signatures that implementing classes must provide. Unlike abstract classes, a class can implement multiple interfaces.
Key Concepts
Key concepts:
- Declare with
interface Name {} - Methods have no body — only signatures
- Implement with
implements InterfaceName - A class can implement multiple interfaces
Defining an Interface
Example:
<?php
interface Serializable {
public function serialize(): string;
public function unserialize(string $data): void;
}
interface Loggable {
public function toLog(): string;
}
// Implementing both interfaces:
class Event implements Serializable, Loggable {
public function __construct(private string $type, private string $data) {}
public function serialize(): string { return json_encode(['type'=>$this->type,'data'=>$this->data]); }
public function unserialize(string $d): void { $a = json_decode($d,true); $this->type=$a['type']; $this->data=$a['data']; }
public function toLog(): string { return "[{$this->type}] {$this->data}"; }
}Interface as Type
Continued:
<?php
interface Cache {
public function get(string $key): mixed;
public function set(string $key, mixed $val, int $ttl = 0): void;
public function delete(string $key): void;
}
// Function accepts any Cache implementation:
function loadUser(int $id, Cache $cache): array {
$key = "user:$id";
$user = $cache->get($key);
if (!$user) {
$user = fetchFromDb($id);
$cache->set($key, $user, 3600);
}
return $user;
}Interface Constants
More depth:
<?php
interface HttpMethods {
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
}
class Router implements HttpMethods {
public function addRoute(string $method, string $path, callable $handler): void {
// ...
}
}
$router = new Router();
$router->addRoute(HttpMethods::GET, '/users', fn() => []);Interface Extending Interface
Practical use:
<?php
interface Readable {
public function read(): string;
}
interface Writable {
public function write(string $data): void;
}
// Interface can extend multiple interfaces:
interface ReadWrite extends Readable, Writable {
public function seek(int $position): void;
}
class FileStream implements ReadWrite {
public function read(): string { return ''; }
public function write(string $d): void { }
public function seek(int $p): void { }
}Dependency Injection with Interfaces
Advanced:
<?php
interface MailerInterface {
public function send(string $to, string $subject, string $body): bool;
}
class SmtpMailer implements MailerInterface {
public function send(string $to, string $subject, string $body): bool {
// real SMTP send
return true;
}
}
class UserService {
public function __construct(private MailerInterface $mailer) {}
public function register(string $email): void {
// save user...
$this->mailer->send($email, 'Welcome!', 'Thanks for joining.');
}
}Pattern
Common PHP Interfaces patterns are used to solve recurring design problems cleanly. Apply them consistently for readable, maintainable code.
When to Use
Knowing when to apply PHP Interfaces is as important as knowing how. Overuse leads to complexity; underuse leads to duplication.
Real-World Example
In real applications, PHP Interfaces appears frequently in frameworks, ORMs, and service layers. Understanding it prepares you to contribute to professional PHP projects.
Common Pitfalls
Avoid these mistakes with PHP Interfaces:
- Classes can implement multiple interfaces but extend only one class
- Interfaces cannot have properties or method bodies (except default in PHP 8)
- Always program to an interface (abstract type) not an implementation
- Use interfaces to enable dependency injection and mocking in tests
Quick Check
How many interfaces can a PHP class implement at once?
Recap: PHP Interfaces
Summary:
- Classes can implement multiple interfaces but extend only one class
- Interfaces cannot have properties or method bodies (except default in PHP 8)
- Always program to an interface (abstract type) not an implementation
- Use interfaces to enable dependency injection and mocking in tests
Frequently asked questions
Is the “Implementing Interfaces” lesson free?
Yes — the full text of “Implementing Interfaces” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Implementing Interfaces”?
Define contracts with interface and implement them in multiple classes. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Implementing Interfaces” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Extending Classes with Inheritance
- Abstract Classes and Methods
- Implementing Interfaces
- Traits for Code Reuse