What are the benefits of using OOP in PHP for organizing and displaying data from multiple database tables?

When working with data from multiple database tables in PHP, using Object-Oriented Programming (OOP) can help organize the code and make it easier to manage and display the data. By creating classes for each database table and using objects to represent the data, you can encapsulate the functionality related to each table and easily access and manipulate the data.

// Example of using OOP in PHP to organize and display data from multiple database tables

class User {
    private $id;
    private $username;
    private $email;

    public function __construct($id, $username, $email) {
        $this->id = $id;
        $this->username = $username;
        $this->email = $email;
    }

    public function getUsername() {
        return $this->username;
    }

    public function getEmail() {
        return $this->email;
    }
}

class UserDB {
    public function getUserById($id) {
        // Database query to fetch user data by id
        $userData = // fetch data from database;

        // Create User object
        $user = new User($userData['id'], $userData['username'], $userData['email']);

        return $user;
    }
}

// Usage example
$userDB = new UserDB();
$user = $userDB->getUserById(1);

echo "Username: " . $user->getUsername() . "<br>";
echo "Email: " . $user->getEmail();