【问题标题】:calling PHP classes from files从文件中调用 PHP 类
【发布时间】: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时,它还不知道PDOConfigdb.php中的PDOConfig
  • 知道了,我从来没有意识到文件是立即执行的,我以为它在执行任何操作之前加载了文件。更改文件的加载顺序以及下面的答案已经解决了这个问题:) 谢谢!

标签: php class methods call


【解决方案1】:

听起来好像问题在于您使用多个 PHP include() / require() 的方式,指向错误的文件夹。当您使用其中任何一个函数时,您包含的文件实质上会将其内容“复制”到包含它们的文件中。

首先,您在index.php 上包含includes/scripts/php/init.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 URL(注意前导反斜杠):

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

希望这会有所帮助! :)

【讨论】:

  • 谢谢 :) 帮助很大!再加上我原来的帖子中关于加载顺序的 cmets,我现在已经加载了脚本 :)
  • 我还建议在 index.php 中创建一个常量,例如 define("ROOT", __DIR__."/") 并将其用于您的包含/要求,例如:require(ROOT . 'includes/scripts/libCore/vars.php');
  • 感谢 Carlos,当我进一步了解项目并构建更多文件时,这实际上可能对我有所帮助。
猜你喜欢
  • 1970-01-01
  • 2012-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-08
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多