我正在关注laracasts.com的教程,我的PDO连接工作正常,但经过一些重构后我得到以下错误'未捕获错误:调用成员函数selectAll()on null'。
这是我的代码
的index.php
<?php
$query = require 'bootstrap.php';
require 'Task.php';
$tasks = $query->selectAll('todos');
require 'index.view.php';
bootstrap.php中
<?php
require 'database/Connection.php';
require 'database/QueryBuilder.php';
return new QueryBuilder(Connection::make());
Connection.php
<?php
class Connection
{
public static function make()
{
try {
return new PDO('mysql:host=127.0.0.1;dbname=mytodo', 'root', '');
} catch (PDOException $e) {
die($e->getMessage());
}
}
}
QueryBuilder.php
<?php
class QueryBuilder
{
protected $pdo;
public function __contruct($pdo)
{
$this->pdo = $pdo;
}
public function selectAll($table)
{
$statement = $this->pdo->prepare("select * from {$table}");
$statement->execute();
return $statement->fetchAll(PDO::FETCH_CLASS);
}
}
有什么想法吗?提前谢谢!