Lesson 24 of 32Article17 min
OOP Review & Backend Patterns
Real apps combine classes, interfaces, and namespaces. Keep models (data), services (logic), and controllers (HTTP) separate.
OOP recap — putting it together
Real apps combine classes, interfaces, and namespaces. Keep models (data), services (logic), and controllers (HTTP) separate.
Real-life example: OOP in a web app is a restaurant — models are ingredients, services are chefs, controllers are waiters taking orders.
Simple service class
<?php
class CourseService {
public function __construct(private PDO $db) {}
public function findAll(): array {
return $this->db
->query("SELECT id, title FROM courses")
->fetchAll(PDO::FETCH_ASSOC);
}
}
?>When to use OOP in PHP
Use classes for anything with state and behavior — users, orders, mailers. Small scripts can stay procedural.
Real-life example: A one-line hello.php does not need OOP. A student portal with login, courses, and payments does.
- Use type hints and return types everywhere
- Prefer dependency injection over global state
- Interfaces make testing and swapping implementations easy
- Next up: MySQL — store your objects in a database