从文件调用PHP类
我正在为我目前正在进行的一个项目建立一个网站。我想在一个单独的文件中初始化一个名为PDOConfig的类,该文件用数据库详细信息填充数组。但是我得到一个错误,说找不到类PDOConfig。为了更好地理解我的意思,以下是相关页面: index.php从文件调用PHP类,php,class,methods,call,Php,Class,Methods,Call,我正在为我目前正在进行的一个项目建立一个网站。我想在一个单独的文件中初始化一个名为PDOConfig的类,该文件用数据库详细信息填充数组。但是我得到一个错误,说找不到类PDOConfig。为了更好地理解我的意思,以下是相关页面: index.php <?php // Start the output buffer and start the session ob_start(); session_start(); // init.php will include all the requ
<?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();
?>
包括/scripts/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';
?>
听起来好像问题在于您使用多个PHPinclude()
/require()
的方式,指向了错误的文件夹。当您使用这两个函数中的任何一个时,您包含的文件基本上会将其内容“复制”到包含它们的文件中
首先,在index.php
上包含includes/scripts/php/init.php
。
init.php
的内容基本上被写入index.php
。因此,当您使用require('libCore/vars.php')
(在init.php
)中,require()
与index.php(而不是init.php
)相关。由于index.php
位于根目录,因此require()
查找[root]/libCore/vars.php
,而不是[root]/includes/scripts/php/libCore/vars.php
要解决这个问题,您应该从includes/scripts/php/
创建includes/scripts/php/
的includes/scripts/php/init.php
,或者更好的是,使用根相对URL(注意前面的反斜杠):
希望这有帮助!:) 阅读关于类自动加载的内容。它会自动将类加载到php应用程序中。您可以使用composer。自动加载器当然是最好的解决方案,但要解决您的特定问题,需要在vars.php
之前加载vars.php
在db.php
之前,因此,当它执行vars.php
时,它还不知道PDOConfig
中的db.php
中的内容,我从未意识到该文件是立即执行的,我以为它在执行任何操作之前加载了文件。更改文件的加载顺序,再加上下面的答案,解决了这个问题:)谢谢!谢谢:)这帮了大忙!再加上我在原始帖子中留下的关于加载顺序的评论,我现在已经加载了脚本:)我还建议在index.php中设置一个常量,比如define(“ROOT”),_udir__;/”
,并将其用于include/reuire,比如:require(ROOT.'include/scripts/libCore/vars.php')代码>谢谢Carlos,当我进一步完成项目并构建更多文件时,这可能会对我有所帮助。
<?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
// 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;
}
}
?>
`require('/libCore/vars.php');`