Can you provide examples of practical applications of OOP in PHP, such as in a login system or data storage?
Issue: Implementing a login system using Object-Oriented Programming in PHP can help organize and manage user authentication efficiently.
<?php
// User class to handle user data
class User {
private $username;
private $password;
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
}
public function getUsername() {
return $this->username;
}
public function getPassword() {
return $this->password;
}
}
// Login class to authenticate users
class Login {
public function authenticate(User $user) {
// Check if username and password match in the database
if ($user->getUsername() === 'admin' && $user->getPassword() === 'password') {
return true;
} else {
return false;
}
}
}
// Usage
$user = new User('admin', 'password');
$login = new Login();
if ($login->authenticate($user)) {
echo 'Login successful';
} else {
echo 'Login failed';
}
?>
Keywords
Related Questions
- How important is it to consult official documentation and community resources when implementing PHP code for sending emails, especially for individuals with limited PHP knowledge?
- What are the different ways to display previous forum posts in a PHP application and what are the pros and cons of each method?
- How can the output of the date() function be manipulated to display the correct time in PHP forums?