Php 无法识别db类函数
获得: 致命错误:在第27行调用未定义的方法UsersController::select application/models/User.php 看起来UsersController没有将select函数发送到我在库文件夹中的db.class.php 正在加载db.class.php,但在本例中不是这样 UserController.php类中的代码是:Php 无法识别db类函数,php,model-view-controller,Php,Model View Controller,获得: 致命错误:在第27行调用未定义的方法UsersController::select application/models/User.php 看起来UsersController没有将select函数发送到我在库文件夹中的db.class.php 正在加载db.class.php,但在本例中不是这样 UserController.php类中的代码是: class User extends Model { /** * Login method * * @todo: u
class User extends Model
{
/**
* Login method
*
* @todo: update last_login_time
* @todo: add hashing
*/
public function user_login() {
$username = $_POST['data']['User']['username'];
$password = $_POST['data']['User']['password'];
$bind = array(
":username" => $username,
);
$result = $this->select("users", "username = :username", $bind);
//Check the password returned from the db against the password entered
if (Bcrypt::checkPassword($password, $result[0]['password']) == true) {
Session::init();
Session::set('user_logged_in', true);
Session::set('user_id', $result[0]['id']);
Session::set('user_name', $result[0]['username']);
Session::set('user_permission', $result[0]['permission']);
Session::set('user_role', $result[0]['role']);
return true;
} else {
return false;
}
}
/**
* Log out process, deletes cookie, deletes session
*
* @todo implement rememberme cookie
*/
public function logout()
{
// set the remember-me-cookie to ten years ago (3600sec * 365 days * 10).
// that's obviously the best practice to kill a cookie via php
// @see http://stackoverflow.com/a/686166/1114320
//setcookie('rememberme', false, time() - (3600 * 3650), '/', COOKIE_DOMAIN);
Session::init();
// delete the session
Session::destroy();
}
}
选择db.class.php中的函数
public function select($table, $where="", $bind="", $fields="*") {
$sql = "SELECT " . $fields . " FROM " . $table;
if(!empty($where))
$sql .= " WHERE " . $where;
$sql .= ";";
return $this->run($sql, $bind);
}
我想这可能是对$this->select的引用,但我正在学习。这个错误的原因是select,据我所知,它不是用户、模型或模型层次结构中其他父对象的方法 而是在db.class.php中 鉴于当前的代码,我建议将DB对象注入用户/模型,然后直接委托给该对象 例如:
class Model {
public function __construct(DB $db) {
$this->db = $db;
}
}
然后,要解决此错误,请在User do中执行以下操作:
注意:这是不完整的。所以你需要填几个空格。既然您正在学习,我建议您阅读:
另外,.class.php是PHP4中使用的旧命名约定。不要这样做。相反,请遵循命名约定。如果您发布的是特定错误,则必须包含完整的错误消息,其中包含行号,至少包括该行及其前一行的代码。此错误告诉我,类UserController没有名为select的方法。这是Laravel吗?select方法在哪里?@N.DeNisse-这是cake php。@JasonMcCreary-刚刚添加。好的-试试看,我可能需要再次阅读书籍,ha haSaw你更新的评论,如果这是CakePHP,这段代码不符合他们的惯例。谢谢-我正在努力理解它,但仍然在学习。
$result = $this->db->select("users", "username = :username", $bind);