我正在尝试学习 OOP,目前正在重写使用登录。我不断收到错误消息:
Fatal error: Call to a member function prepare() on a non-object in /var/www/new.php on line 22
有人可以指出我做错了什么吗?除了明显的错误之外,到目前为止,这看起来是否像良好的 OOP 编码实践?
<?php
ini_set('display_errors', TRUE);
require 'resources/library/DB.php';
class DataBase
{
private $conn = null;
public function __construct($conn)
{
$this->conn = $conn;
}
public function setConn($conn)
{
$this->conn = $conn;
return $this;
}
public function retrievePassword($userNAme)
{
$stmt = $this->conn->prepare('SELECT `password` FROM `users` WHERE `userName`= :userName');
$stmt->bindValue(':userName', $userNAme);
$stmt->execute();
$salt = $stmt->fetchColumn();
return $salt;
}
}
$db = new DataBase($conn);
echo $db->retrievePassword('testuser');
?>