我一直忙于尝试创建自己的框架(以便在这方面变得更有经验),并且偶然发现了一个我无法通过搜索 google 修复的错误...哇...
我想从数据库中获取数据,放在一个对象/类中。我以前做过,用的是我在学校学到的不同方式,但我想对其进行调整并使其更具动态性,以便我可以在我的框架中使用它。
我偶然发现的问题如下:
SQLSTATE[HY000]: General error: could not call class constructor on line 96
这是我的数据库类中的函数:
public function getObject($query, $classRootPath)
{
try {
//Check if slashes are double already and make them if not
if(!strpos($classRootPath, "\\\\")) {
$classRootPath = str_replace("\\","\\\\",$classRootPath);
}
$statement = $this->pdo->prepare($query);
$statement->execute(\PDO::FETCH_CLASS, "Campers\\Camper"); // I want this path to be $classRootPath once it is working with this dummy data
return $statement->fetchAll();
// return $this->pdo->query($query)->fetchAll(\PDO::FETCH_CLASS, "Campers\\Camper");
} catch (\PDOException $e) {
throw new \Exception("DB receive object failed: " . $e->getMessage());
}
}
该函数嵌套在Database
中,类名为Database
/Database.php
以下类嵌套在 Campers
中,名为 Camper.php
class Camper {
public $ID, $date, $camperID;
public function __construct($ID, $date, $camperID)
{
$this->ID = $ID;
$this->date = $date;
$this->camperID = $camperID;
}
}
我认为这不起作用的唯一原因是调用 "Campers\\Camper"
是在 Database
之上调用的,但我不知道不知道如何逃避。我尝试使用 ..\
但我得到了错误,这是我能得到的最接近的。这里虽然可以找到类,但是找不到Camper
的构造函数...
我已经测试过我的数据库类/连接是否有效,所以这不是问题。
我的表结构与我的 Campers
类构造函数匹配。
最佳答案
来自PSR-4 spec :
The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.
您可能无法按原样实例化 Camper
类。 PSR-4 期望您的文件名与类匹配。它应该位于 framework/Campers/Camper.php
。
关于php - 无法调用类构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29757531/