In what scenarios is it recommended to use classes instead of procedural code in PHP for managing variables and functions?

When you have a set of related variables and functions that need to be grouped together, it is recommended to use classes instead of procedural code in PHP. Classes allow for better organization, encapsulation, and reusability of code. They also help in avoiding naming conflicts and provide a clear structure for your code.

class User {
    public $name;
    public $email;

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

    public function greet() {
        return "Hello, my name is " . $this->name;
    }
}

$user1 = new User("John Doe", "john.doe@example.com");
echo $user1->greet();