2016-05-03 10 views
0

我在我的項目中使用了未定義的常量錯誤。這裏是設置在我的項目中使用PHP定義的常量錯誤PDO

常數是;

define('VIEW_ROOT_ADMIN', '/views/admin'); 

並從稱爲文件是

<?php 
$mode = file_get_contents('../app/mode.php'); 

require '../app/' . $mode . '.php'; 

require VIEW_ROOT_ADMIN . '/index.php'; 

這是錯誤發生時;

Notice: Use of undefined constant VIEW_ROOT_ADMIN - assumed 'VIEW_ROOT_ADMIN' 

的這一奇怪的是,在這裏工作正常,在這個文件結構如下

define('VIEW_ROOT', '/views'); 

<?php 
$mode = file_get_contents('app/mode.php'); 

require 'app/' . $mode . '.php'; 

require VIEW_ROOT . '/index.php'; 

任何幫助將是不錯的:-)

這是文件結構

ROOT---- 
    |--admin 
     |--index.php 
    |--app 
     |--development.php //Here are the contants define('VIEW_ROOT', '/views'); and define('VIEW_ROOT_ADMIN', '/views/admin'); 
     |--mode.php 
     |--production.php 
    |--views 
     |--admin 
      |--index.php 
     |--index.php 
    index.php 
+1

'$ mode = file_get_contents('../ app/mode.php'); 需要'../app/'。 $模式。 '.php';'沒有任何意義。 –

+0

這只是模式的開發或生產環境 – John

+0

是啊......無論如何。您提出了很多問題,但尚未接受任何答案。這個網站是雙向的。如果你期望得到幫助,你應該通過接受他們的回答來獎勵那些幫助你的人。閱讀[接受答案如何工作?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)並開始回饋社區。另外,你會得到重要的分數,那些如此優雅地給予他們空閒時間來幫助你的人。 –

回答

0

你應該

<?php 
require_once('/app/mode.php'); // this sets $mode = 'development' or similar 
require_once('/app/' . $mode . '.php'); // this sets the constants 

之後,常量定義,你可以使用它:

require_once(VIEW_ROOT_ADMIN . '/index.php'); 

我贊成需要使用require_once()(),以防止文件被包含不止一次。

+0

是的app/mode.php只是基本狀態是否開發或生產 – John

+0

葉,但問題仍然存在'<?php require_once('../ app/mode.php');' – John

+0

在您的文件結構沒有mode.php文件,所以你找不到它。 –