İleri OOP: SOLID ve Domain Driven Design
SOLID prensipleri, bağımlılık tersine çevirme (DI), arayüz tabanlı tasarım, aggregate root, value object, repository pattern ve DDD kavramlarının PHP uygulamaları. Test edilebilir, genişletilebilir ve sürdürülebilir domain modelleri oluşturma.
Repository ve DI örneği
<?php
interface UserRepository { public function findById(int $id): ?User; }
class PdoUserRepository implements UserRepository {
public function __construct(private \PDO $pdo) {}
public function findById(int $id): ?User {
$stmt = $this->pdo->prepare('SELECT id,name FROM users WHERE id = ?');
$stmt->execute([$id]);
$row = $stmt->fetch();
return $row ? new User((int)$row['id'], $row['name']) : null;
}
}
?>