从文件中调用PHP类

时间:2017-03-27 20:58:57

标签: 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 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?我已经看到了一些关于使用称为“命名空间”的东西来做这件事但是我认为我需要首先更好地掌握课程,因为我仍然非常业余。感谢

1 个答案:

答案 0 :(得分:1)

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

首先,您将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/init.php提出require() includes/scripts/php/,或者更好的是,使用 root-relative 网址(请注意前导反斜杠):

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

希望这有帮助! :)