我尝试构建一个类似 Laravel 的查询构建器,但我遇到了问题。
致命错误:在第 31 行的 C:\xampp\htdocs\tutorial\public\classes\DB.php 中的非对象上调用成员函数 fetch()
class DB {
private $_pdo,
$_query,
$_results = array(),
$_count = 0;
public static $instance;
public static function getInstance(){
if(!isset(self::$instance)){
self::$instance = new DB();
}
return self::$instance;
}
public function __construct(){
try {
$this->_pdo = new PDO('mysql:host=localhost;dbname=query', 'root', '');
$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
public function query($sql){
if($this->_pdo = $this->_pdo->query($sql)){
while($row = $this->_query->fetch(PDO::FETCH_OBJ)){
$this->_results[] = $row;
}
}
return $this;
}
public function results(){
return $this->_results;
}
}