php 在 null 上调用成员函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42863856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function on null?
提问by Still Learning
I have done all sorts of things to understand what is actually wrong with the code, but I just couldn't figure out what was causing this error.
我已经做了各种各样的事情来了解代码的实际问题,但我无法弄清楚是什么导致了这个错误。
Below is class called Topic
:
下面是名为的类Topic
:
class Topic {
// Init DB variables
private $db;
/*
* Constructor
*/
public function __contruct() {
$this->db = new Database();
}
/*
* Get All Topics
*/
public function getAllTopics() {
$this->db->query("SELECT topics.*, users.username,
users.avatar, categories.name
FROM topics
INNER JOIN users
ON topics.user_id = users.id
INNER JOIN categories
ON topics.category_id = categories.id
ORDER BY create_date DESC");
// Assign the results
$results = $this->db->resultset();
return $results;
}
}
When I create an object of class Topic
,
当我创建一个类的对象时Topic
,
$topic = new Topic();
$results = $topic->getAllTopics();
there is always an error like this :
总是有这样的错误:
Fatal error: Call to a member function query() on null in /Applications/XAMPP/xamppfiles/htdocs/forumproject/libraries/Topic.php on line 25 i.e. $this->db->query(...)
致命错误:在第 25 行的 /Applications/XAMPP/xamppfiles/htdocs/forumproject/libraries/Topic.php 中的 null 上调用成员函数 query(),即 $this->db->query(...)
The Database
class is working perfectly as I have run tests independently for that class, and there is no problem with it.
该Database
课程运行良好,因为我已经为该课程独立运行了测试,并且没有任何问题。
Here is the Database class :
这是数据库类:
<?php
/*
* Using PDO for the first time in my life.
*/
class Database {
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct() {
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instance
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch the errors using exceptions handler
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if(isnull($type)){
switch(true){
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default :
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
public function rowCount(){
return $this->stmt->rowCount();
}
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
public function endTransaction(){
return $this->bbh->commit();
}
public function cancelTransaction(){
return $this->dbh->rollBack();
}
}
?>
回答by gmc
You have a typo in public function __contruct()
, it should be public function __construct() {
(you are missing an s)
你有一个错字public function __contruct()
,它应该是public function __construct() {
(你错过了一个 s)