SyntaxStudy
Sign Up
PHP Intermediate 4 min read

Interface Best Practices

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.

Example
<?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;
}
Pro Tip

Name interfaces for what they represent from the caller's perspective, not the implementer's internal mechanics.