无法在 PHP MVC 框架中实例化 PDO 对象
谁能帮我解决这个问题吗?
我在单例类中有这个函数。它给我返回的错误是它找不到该类。
首先我认为这与自动加载器有关,但我做了 spl_autoload_unregister('libloader') 并且它仍然给出相同的错误?
主机正在运行 php 5。
public static function getInstantie()
{
if (!self::$instantie)
{
$config = config::getInstantie();
$db_type = $config->config_waarden['database']['db_type'];
$hostnaam = $config->config_waarden['database']['db_hostnaam'];
$dbnaam = $config->config_waarden['database']['db_naam'];
$db_wachtwoord = $config->config_waarden['database']['db_wachtwoord'];
$db_gebruikersnaam = $config->config_waarden['database']['db_gebruikersnaam'];
$db_poort = $config->config_waarden['database']['db_poort'];
self::$instantie = new PDO("$db_type:host=$hostnaam;port=$db_poort;dbname=$dbnaam",$db_gebruikersnaam, $db_wachtwoord);
self::$instantie-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instantie;
}
谢谢,理查德
Can anyone help me with this?
I have this function in a singleton class. The error it is giving me back is that it cannot find the class.
First I thought it had something to do with the autoloader, but I did spl_autoload_unregister('libloader') and it still gives the same error?
The host is running php 5.
public static function getInstantie()
{
if (!self::$instantie)
{
$config = config::getInstantie();
$db_type = $config->config_waarden['database']['db_type'];
$hostnaam = $config->config_waarden['database']['db_hostnaam'];
$dbnaam = $config->config_waarden['database']['db_naam'];
$db_wachtwoord = $config->config_waarden['database']['db_wachtwoord'];
$db_gebruikersnaam = $config->config_waarden['database']['db_gebruikersnaam'];
$db_poort = $config->config_waarden['database']['db_poort'];
self::$instantie = new PDO("$db_type:host=$hostnaam;port=$db_poort;dbname=$dbnaam",$db_gebruikersnaam, $db_wachtwoord);
self::$instantie-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instantie;
}
thanks, Richard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,通过一组数据库驱动程序启用 PDO:
http://au。 php.net/manual/en/pdo.installation.php
但是您正在使用的 PHP 版本可能会禁用它。自动加载对于是否找到 PDO 类没有影响。
创建一个 PHP 信息文件并检查 PDO 部分是否存在。如果没有,那么您的问题很可能是因为它没有内置到您的 php 安装中。
PDO is enabled by default with a set of database drivers:
http://au.php.net/manual/en/pdo.installation.php
But the build of PHP you are working with could have it disabled. Autoloading will have no effect on whether or not the PDO class will be found.
Create a PHP info file and check to see if the PDO section exists. If it doesn't, then your issue is most likely because it wasn't built into your php installation.
该函数是在扩展 PDO 的类中吗?如果没有,您是否可以尝试在扩展 PDO 的类中创建该函数,而不是使用关键字
parent
调用函数,而不是self
?is that function inside of a class that extends PDO? If not, can you try to make that function inside of a class that extends PDO and instead of
self
call the functions using keywordparent
?