How can the code structure be optimized to adhere more closely to the principles of the MVC pattern in PHP?

To optimize the code structure to adhere more closely to the principles of the MVC pattern in PHP, separate the concerns of the Model, View, and Controller. Move database interactions and business logic to the Model, presentation logic to the View, and request handling to the Controller.

// Model (e.g. User.php)
class User {
    public function getUserById($id) {
        // Database query to fetch user details
    }
}

// View (e.g. user_view.php)
class UserView {
    public function displayUserDetails($user) {
        // Display user details in HTML
    }
}

// Controller (e.g. UserController.php)
class UserController {
    public function getUserDetails($id) {
        $userModel = new User();
        $user = $userModel->getUserById($id);

        $userView = new UserView();
        $userView->displayUserDetails($user);
    }
}