How does object-oriented programming (OOP) approach compare to using functions for modularizing PHP code, and what are the advantages of using classes for this purpose?

Object-oriented programming (OOP) allows for better organization and structure of code by encapsulating data and behavior within objects. This approach provides a more modular and reusable way to design applications compared to using functions alone. Classes in OOP can contain both data (properties) and functions (methods), making it easier to manage and maintain code.

// Example of using classes for modularizing PHP code

class User {
    private $username;
    private $email;

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

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

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

$user1 = new User('john_doe', 'john.doe@example.com');
echo $user1->getUsername(); // Output: john_doe
echo $user1->getEmail(); // Output: john.doe@example.com