id = $id; $this->name = $name; $this->email = $email; } public function getId(){ return $this->id; } public function getName(){ return $this->name; } public function getEmail(){ return $this->email; } } ?>
db = $db; } public function getUserById($id){ $query = "SELECT * FROM users WHERE id = :id"; $stmt = $this->db->prepare($query); $stmt->bindParam(':id', $id); $stmt->execute(); $user = $stmt->fetch(PDO::FETCH_ASSOC); return new User($user['id'], $user['name'], $user['email']); } } $db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password'); $userRepository = new UserRepository($db); $user = $userRepository->getUserById(1); ?>In this example, we create a UserRepository model that retrieves a user's data from a database and returns a User model object. Package Library: PDO (PHP Data Objects) is used to interact with the database.