我发现了PDO,并按照教程开发了一个博客项目。我现在在创建自动加载器后连接到数据库。从我创建查询以检索结果的那一刻起,我就出现以下错误:
注意:未定义的变量:class_name in 第18行/Applications/MAMP/htdocs/php/Bilal/app/Autoloader.php
警告:require(/Applications/MAMP/htdocs/php/Bilal/app/.php):无法打开流:没有此类文件或目录 第18行/Applications/MAMP/htdocs/php/Bilal/app/Autoloader.php
致命错误:require():无法打开所需的' /Applications/MAMP/htdocs/php/Bilal/app/.php' (include_path ='。:/ Applications / MAMP / bin / php / php7.2.1 / lib / php')in 第18行/Applications/MAMP/htdocs/php/Bilal/app/Autoloader.php
这是我的文件(Autoloader.php):
<?php
namespace App;
/**
* Class Autoloader
*/
class Autoloader{
static function register(){
spl_autoload_register(array(__CLASS__, 'autoload'));
}
static function autoload($class){
if (strpos($class, __NAMESPACE__ . '\\') === 0){
$class = str_replace(__NAMESPACE__ . '\\', '', $class);
$class = str_replace('\\', '/', $class);
require __DIR__ . '/'. $class_name . '.php';
}
}
}
这是我的文件(Database.php)
<?php
namespace App;
use \PDO;
class Database{
private $db_name;
private $db_user;
private $db_pass;
private $db_host;
private $pdo;
public function __construct($db_name, $db_user = 'root', $db_pass = 'root', $db_host = 'localhost'){
$this->db_name = $db_name;
$this->db_user = $db_user;
$this->db_pass = $db_pass;
$this->db_host = $db_host;
}
/**
* Accesser for connection to database
*/
private function getPDO(){
if($this->pdo === nul){
$pdo = new PDO('mysql:host='.$this->db_host.';dbname='.$this->db_name, $this->db_user, $this->db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->pdo = $pdo;
}
return $this->pdo;
}
public function query($statement){
$req = $this->getPDO()->query($statement);;
// On exécute la requète avec fetchAll
$datas = $req>fetchAll(PDO::FETCH_OBJ);
return $datas;
}
}
请找不到问题并寻求帮助。