从文件调用 PHP 类

calling PHP classes from files

我正在为我目前正在进行的项目构建一个网站。我想在一个单独的文件中初始化一个名为 PDOConfig 的 class,该文件用数据库详细信息填充一个数组。但是我收到一条错误消息,说 Class PDOConfig not found。为了更好地理解我的意思,这里是有问题的页面:

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();
?>

includes/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';
?>

includes/scripts/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


?>

includes/scripts/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 就可以完成这项工作?我已经看到一些关于使用称为 "namespaces" 的东西来做到这一点,但是我认为我需要首先更好地掌握 classes,因为我在这方面仍然非常业余。谢谢

听起来好像问题出在您使用多个 PHP include() / require() 的方式上,指向错误的文件夹。当您使用这些功能中的任何一个时,您包含的文件基本上 'copy' 将它们的内容放入包含它们的文件中。

首先,您将 includes/scripts/php/init.php 添加到 index.phpinit.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/init.php require(),或者更好的是,使用 root-relative URL(注意前导反斜杠):

`require('/libCore/vars.php');`

希望这对您有所帮助! :)