How does the concept of MVC in PHP differ from the traditional three-tier architecture?

In traditional three-tier architecture, the presentation, business logic, and data access layers are tightly coupled, making it difficult to maintain and update the application. MVC (Model-View-Controller) separates these concerns, allowing for a more modular and organized codebase. In PHP, implementing MVC involves creating separate classes for models, views, and controllers, which helps improve code reusability and maintainability.

// Example of implementing MVC in PHP

// Model class
class User {
    public function getUserById($id) {
        // Database query to retrieve user data
    }
}

// View class
class UserView {
    public function displayUser($userData) {
        // Display user data in a view template
    }
}

// Controller class
class UserController {
    public function showUser($id) {
        $userModel = new User();
        $userData = $userModel->getUserById($id);

        $userView = new UserView();
        $userView->displayUser($userData);
    }
}

// Usage
$userId = 1;
$userController = new UserController();
$userController->showUser($userId);