What are the benefits of using object-oriented programming in PHP for handling variables and functions?

Object-oriented programming in PHP allows for better organization and encapsulation of variables and functions. It helps in creating reusable code through the use of classes and objects. Additionally, it promotes code reusability, modularity, and flexibility in managing complex applications.

class User {
    private $username;
    
    public function setUsername($name) {
        $this->username = $name;
    }
    
    public function getUsername() {
        return $this->username;
    }
}

$user = new User();
$user->setUsername('JohnDoe');
echo $user->getUsername(); // Output: JohnDoe