找不到 PHP 命名空间 PDO
2022-08-31 00:00:34
我遇到了一个我不幸无法解决的问题。我创建了一个包含以下内容的 into:database class
app/db/mysql/database.php
<?php
namespace App\Database;
use Symfony\Component\Yaml\Yaml;
class Database{
private static $connection = null;
private function __construct( $host, $base, $user, $pass ){
try{
self::$connection = new PDO("mysql:host=$host;dbname=$base", $user, $pass);
}catch(PDOException $e){
die($e->getMessage());
}
}
public static function get(){
if( self::$connection !== null ){
return self::$connection;
}
$yaml = Yaml::parse(file_get_contents(realpath('./app') . '/database.yml'));
self::$connection = new Database( $yaml['host'], $yaml['base'], $yaml['user'], $yaml['pass'] );
}
}
使用作曲家,我自动加载这个类:
{
"autoload" : {
"classmap" : [
"app/libraries",
"app/db"
]
}
}
这会产生一个这样的:autoload_classmap.php
return array(
'App\\Database\\Database' => $baseDir . '/app/db/mysql/database.php',
'App\\Libraries\\Parser' => $baseDir . '/app/libraries/Parser.php',
);
现在,当一切正常时,我总是收到与PDO相关的错误:
Fatal error: Class 'App\Database\PDO' not found in /var/www/my_application/app/db/mysql/database.php on line 24
我认为问题来自,因为当我将类放入索引页时,我没有任何错误。PDO 已安装并正常工作。namespace