How can the code provided be improved in terms of object-oriented programming principles and best practices?
The code can be improved by encapsulating the properties and methods within a class to adhere to object-oriented programming principles. This will help in organizing the code, improving reusability, and maintaining a clear structure.
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;
}
public function setUsername($username) {
$this->username = $username;
}
public function setEmail($email) {
$this->email = $email;
}
}
$user = new User('john_doe', 'john.doe@example.com');
echo $user->getUsername(); // Output: john_doe
echo $user->getEmail(); // Output: john.doe@example.com
$user->setUsername('jane_smith');
$user->setEmail('jane.smith@example.com');
echo $user->getUsername(); // Output: jane_smith
echo $user->getEmail(); // Output: jane.smith@example.com