When working with PHP classes and objects, what are the best practices for structuring code to ensure proper instantiation and usage of objects?

When working with PHP classes and objects, it is important to follow best practices to ensure proper instantiation and usage of objects. One common practice is to use a constructor method to initialize object properties when an object is created. Additionally, it is recommended to use access modifiers like public, private, and protected to control the visibility of properties and methods within a class.

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;
    }
}

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