愚蠢的问题,我是PHP的新手,参加了一些codeacademy课程,但课程部分对我来说还不是很清楚。我正在尝试解决它,并修改上面的代码。
以下是一个类,它检查用户是否已登录,如果是,则创建用户会话。
class Login
{
/**
* @var object The database connection
*/
private $db_connection = null;
/**
* @var array Collection of error messages
*/
public $errors = array();
/**
* @var array Collection of success / neutral messages
*/
public $messages = array();
/**
* the function "__construct()" automatically starts whenever an object of this class is created,
* you know, when you do "$login = new Login();"
*/
public function __construct()
{
// create/read session, absolutely necessary
session_start();
// check the possible login actions:
// if user tried to log out (happen when user clicks logout button)
if (isset($_GET["logout"])) {
$this->doLogout();
}
// login via post data (if user just submitted a login form)
elseif (isset($_POST["login"])) {
$this->dologinWithPostData();
}
}
/**
* log in with post data
*/
private function dologinWithPostData()
{
// check login form contents
if (empty($_POST['user_name'])) {
$this->errors[] = "Username field was empty.";
} elseif (empty($_POST['user_password'])) {
$this->errors[] = "Password field was empty.";
} elseif (!empty($_POST['user_name']) && !empty($_POST['user_password'])) {
// create a database connection, using the constants from config/db.php (which we loaded in index.php)
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// change character set to utf8 and check it
if (!$this->db_connection->set_charset("utf8")) {
$this->errors[] = $this->db_connection->error;
}
// if no connection errors (= working database connection)
if (!$this->db_connection->connect_errno) {
// escape the POST stuff
$user_name = $this->db_connection->real_escape_string($_POST['user_name']);
// database query, getting all the info of the selected user (allows login via email address in the
// username field)
$sql = "SELECT user_name, user_email, user_password_hash
FROM sales_users
WHERE user_name = '" . $user_name . "' OR user_email = '" . $user_name . "';";
$result_of_login_check = $this->db_connection->query($sql);
// if this user exists
if ($result_of_login_check->num_rows == 1) {
// get result row (as an object)
$result_row = $result_of_login_check->fetch_object();
// using PHP 5.5's password_verify() function to check if the provided password fits
// the hash of that user's password
if (password_verify($_POST['user_password'], $result_row->user_password_hash)) {
// write user data into PHP SESSION (a file on your server)
$_SESSION['user_name'] = $result_row->user_name;
$_SESSION['user_email'] = $result_row->user_email;
$_SESSION['user_login_status'] = 1;
} else {
$this->errors[] = "Wrong password. Try again.";
}
} else {
$this->errors[] = "This user does not exist.";
}
} else {
$this->errors[] = "Database connection problem.";
}
}
}
/**
* perform the logout
*/
public function doLogout()
{
// delete the session of the user
$_SESSION = array();
session_destroy();
// return a little feeedback message
$this->messages[] = "You have been logged out.";
}
/**
* simply return the current state of the user's login
* @return boolean user's login status
*/
public function isUserLoggedIn()
{
if (isset($_SESSION['user_login_status']) AND $_SESSION['user_login_status'] == 1) {
return true;
}
// default return
return false;
}
}
我想要做的是扩展这个类,并使用它从数据库中获取一些其他数据。所以我写道:
class Price extends Login
{
public function getPrice()
{
$sql = "SELECT *
FROM sales_pricelist";
$result = $Login->db_connection->query($sql);
$result = $result->fetch_object();
echo $result->product_name;
}
}
我知道这一部分根本不正确。但它说明了我想要实现的目标。所以问题是,正确的版本应该怎么样?我希望在看到确切的例子时我能更好地学习它。
编辑#1:
此部分$result = $Login->db_connection->query($sql);
是否正确?这基本上意味着,我正在从Login类中定位db_connection吗?
编辑#2:
感谢您的所有评论。
现在,我的Price
课程如下:
class Price extends Login
{
public function __construct() {
parent::__construct();
}
public function getPrice()
{
$sql = "SELECT *
FROM sales_pricelist";
$result = $this->db_connection->query($sql);
$result = $result->fetch_object();
echo $result->product_name;
}
}
我正试图像这样使用getPrice
:
$price = new Price();
$price->getPrice();
但缺少一些东西。这里有什么错误吗?
答案 0 :(得分:2)
使用$this
引用当前对象。当子类继承parent方法和属性时,您将访问db属性:
$result = $this->db_connection->query($sql);
根据评论进行编辑
如上所述,$db_connection
具有私有范围,因此子类无法访问。如果您希望在子类中访问它,您需要将其更改为protected
或public
,或者在父级中编写具有受保护或公共范围的getter方法:
protected function getDb()
{
return $this->db_connection;
}
重新parent::__construct()
除非您覆盖子类中的方法(通过在其中编写__construct()
方法,否则将自动调用父构造函数。如果这样做,您将需要按照所述方式调用父组件;
初始化数据库连接
如上所述,db连接在dologinWithPostData()
方法中初始化。为了使它对其他方法有用,在父构造函数中初始化它会更有意义。
一般说明 这违反了各种良好的OOP原则,例如封装。学习一个体面的现代MVC框架可能会比遵循随机互联网教程更好地奠定基础
答案 1 :(得分:1)
乍一看,这对我来说是正确的。你需要的是调用父构造函数。在您的Price类中,以便您可以运行您的登录代码。
function __construct() {
parent::__construct();
}