Interface Best Practices
Keep interfaces small and focused, name them clearly (suffix with Interface or use adjectives), and design them from the consumer's perspective, not the implementer's.
Keep interfaces small and focused, name them clearly (suffix with Interface or use adjectives), and design them from the consumer's perspective, not the implementer's.
<?php
// Good naming conventions
interface LoggerInterface {} // Suffix approach
interface Loggable {} // Adjective approach (preferred by Laravel)
interface CanSendEmail {} // Action approach
// Design from consumer perspective
// BAD: exposes internal complexity
interface DatabaseInterface {
public function runRawQuery(string $sql, array $params): array;
public function beginTransaction(): void;
public function commitTransaction(): void;
// ... 20 more methods
}
// GOOD: only what consumers need
interface UserRepositoryInterface {
public function find(int $id): ?User;
public function save(User $user): void;
public function findByEmail(string $email): ?User;
}
Name interfaces for what they represent from the caller's perspective, not the implementer's internal mechanics.