【发布时间】:2017-08-20 17:59:57
【问题描述】:
我正在为我目前正在进行的项目建立一个网站。我想在一个单独的文件中初始化一个名为 PDOConfig 的类,该文件用数据库详细信息填充数组。但是,我收到一条错误消息,提示找不到 Class PDOConfig。为了更好地理解我的意思,这里是有问题的页面:
index.php
<?php
// Start the output buffer and start the session
ob_start();
session_start();
// init.php will include all the required core libraries
include_once('includes/scripts/php/init.php');
?>
<!-- html code -->
<?php
ob_end_flush();
?>
包括/脚本/php/init.php
<?php
// init.php
// This file includes all the core libraries and, if required, customer and admin libraries.
#TODO: Include core libraries
require('libCore/vars.php');
require('libCore/db.php');
require('libCore/pages.php');
require('libCore/catalogue.php');
#TODO: Check if customer has logged in. If so include customer libraries.
#TODO: Check if a member of staff has logged in. If so include admin libraries.
#TODO: Log File Variables
$dblogfile = logs/db_log.txt';
?>
包括/脚本/php/libCore/vars.php
<?php
// vars.php
// This file contains all of the site variables.
# Database Variables
$db_conf = new PDOConfig();
$db_conf->write('dsn.host', 'localhost');
$db_conf->write('db.username', '/*Username Here*/');
$db_conf->write('db.password', '/*Password Here*/');
$db_conf->write('dsn.dbname', 'A1Newsagents');
$db_conf->write('dsn.driver', 'sqlsrv');
$db_conf->write('dsn.charset', 'utf8');
#TODO: Form Variables
#TODO: Page Title Variables
?>
包括/脚本/php/libCore/db.php
<?php
// libCore/db.php
// This file contains all of the classes and functions required for database interaction.
class PDOConfig
{
// This class sets up an array that stores the configuration for the DSN and db username/password
static $confarray;
public static function read($conf_name)
{
return self::$confarray[$conf_name];
}
public static function write($conf_name, $conf_value)
{
self::$confarray[$conf_name] = $conf_value;
}
}
class DBCore
{
// This class sets up the database connection and controls database functionality
public $dbc;
private static $dbinst;
private function __construct()
{
// Build DSN
$dsn = PDOConfig::read('dsn.driver') . ':Server=' . PDOConfig::read('dsn.host') . ';Database=' . PDOConfig::read('dsn.dbname');
// Get db username and password
$dbuser = PDOConfig::read('db.username');
$dbpass = PDOConfig::read('db.password');
// Set the error mode to EXCEPTION, turn off PREPARE EMULATION and set the fetch mode to FETCH_ASSOC
$dbopts = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC);
// Start a new databse connection instance
$this->dbc = new PDO($dsn, $dbuser, $dbpass/*, $dbopts*/);
}
public static function DBInstance()
{
if (!isset(self::$dbinst))
{
$dbobject = __CLASS__;
self::$dbinst = new $dbobject;
}
return self::$dbinst;
}
}
?>
有没有一种方法可以让我不必在每个文件中都包含 db.php 来完成这项工作?我已经看到一些关于使用称为“命名空间”的东西来做到这一点,但是我认为我需要先更好地掌握类,因为我对这些东西仍然非常业余。谢谢
【问题讨论】:
-
了解类自动加载。它会自动将类加载到您的 php 应用程序中。您可以为此使用作曲家。
-
自动加载器当然是最好的解决方案,但要解决您的具体问题,需要
db.php之前vars.php -
你在
db.php之前已经加载了vars.php,所以当它执行vars.php时,它还不知道PDOConfig在db.php中的PDOConfig -
知道了,我从来没有意识到文件是立即执行的,我以为它在执行任何操作之前加载了文件。更改文件的加载顺序以及下面的答案已经解决了这个问题:) 谢谢!