簡體   English   中英

PHP錯誤:未定義CONSTANT,但已定義,並且Value為空?

[英]PHP Error is Saying CONSTANT is not Defined but its already defined and Value is Empty?

我在Web設計項目中進行了以下設置(體系結構)。 由於某種原因,這不起作用。

我有index.php頁面,頂部包含以下內容

require_once("includes/init.php");

然后在我的init.php中

require_once('config.php');
require_once('functions.php');
ob_start();
spl_autoload_register('load_api');
$db      = new Database();
$db->connect();

在我的配置文件中,我定義了以下內容

 define('DOC_ROOT', $_SERVER['DOCUMENT_ROOT']."/websitename");
define('CLASS_LIB', DOC_ROOT . "/classes");

在我的自動加載功能中,包含

function load_api($class){
    if(!file_exists(CLASS_LIB."/".$class. '.class.php') )
        return false;
    require_once( CLASS_LIB."/".$class. '.class.php');
    return true;
} 

當我在xamp / xdebugger中運行此設計結構時,它會出錯

( ! ) Notice: Use of undefined constant CLASS_LIB - assumed 'CLASS_LIB' in D:\websitename\includes\functions.php on line 7
Call Stack
#   Time    Memory  Function    Location
1   0.0009  144128  {main}( )   ..\index.php:0
2   0.0020  155632  require_once( 'D:\websitenae\includes\init.php' )   ..\index.php:2
3   0.0129  400360  spl_autoload_call ( 'Database' )    ..\index.php:6
4   0.0129  400408  load_api( $class = 'Database' ) ..\index.php:0

( ! ) Fatal error: Class 'Database' not found in D:\websitename\includes\init.php on line 6
Call Stack
#   Time    Memory  Function    Location
1   0.0009  144128  {main}( )   ..\index.php:0
2   0.0020  155632  require_once( 'D:\websitename\includes\init.php' )  ..\index.php:2

錯誤似乎是未檢測到CLASS_LIB,但是我無法弄清楚為什么,因為它似乎已經正確定義並在此腳本之前加載,再加上我在代碼中也看不到重復包含...任何想法都值得贊賞嗎?

最終我找到了它,任何人都可能發生,因此我將其發布為解決方案。

當我運行此程序時,require()函數沒有任何錯誤,因為它正在加載配置文件,但是我發現它沒有在給定位置加載正確的配置文件,

取而代之的是它進入PEAR包括位置並加載名為“ config.php”的確切名稱文件,這就是為什么require並沒有給我錯誤,這就是為什么未加載我的配置文件vars的原因。

所以要修復,我必須為我的配置指定顯式​​路徑,如下所示

require_once(__DIR__.'/config.php');

那解決了問題:)非常棘手

據我所知ob_start(); 必須在最上面並且ob_end_flush(); 在底部。

ob_start(); // top
//
//
ob_end_flush(); // bottom

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM