在 php 中使用未定义的常量 erorr


Use of undefined constant erorr in php

当我将代码切换到另一个系统时,我收到错误(在代码下面提到),

配置:

操作系统 : 视窗 7PHP服务器:Xampp

请帮我解决这个问题。 提前谢谢。

法典:

<?php
class settings{
    public $theme;
    public $db_pf;
    function __construct(){
        $this->theme="default";
        $this->db_pf="task_";
        $this->paths();
        $this->abbr();
        $this->errors();
        $this->enable_db_config();
    }
    function paths(){
        define(ROOT,$_SERVER['DOCUMENT_ROOT']);
        define(BP,"/task/");
        define(ABSPATH,"/task/ta-admin/");
        define(ADMPATH,"/task/administrator/");
        define(INCLUDE_PATH,ABSPATH."includes/");
        define(MODULE_PATH,ABSPATH."modules/");
        define(CONTENT_PATH,ABSPATH."contents/");
        define(THEME_PATH,ABSPATH."templates/".$this->theme."/");
        define(STYLE_PATH,THEME_PATH."styles/");
        define(IMAGE_PATH,THEME_PATH."images/");
        define(JS_PATH,BP."assets/js/");
        define(JQ_PATH,BP."assets/jq/");
    }
    function enable_db_config(){
        include(ROOT.BP."ta-includes/class-db-config.php");
    }
    function abbr(){
        define(task_title,"Admin-Quit Tasker-");
        define(LOGIN_TITLE," Administrator Login...");
        define(SLOGAN,"Enabling Solutions to your Tasks...");
    }
    function errors(){
        define(NOTFOUND,"Page not found");
        define(GOBACK," please press ok to go back...");    
    }
}
$obj_settings=new settings();
?>

错误:

Notice: Use of undefined constant ROOT - assumed 'ROOT' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 14
Notice: Use of undefined constant BP - assumed 'BP' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 15
Notice: Use of undefined constant ABSPATH - assumed 'ABSPATH' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 16
Notice: Use of undefined constant INCLUDE_PATH - assumed 'INCLUDE_PATH' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 17
Notice: Use of undefined constant CONTENT_PATH - assumed 'CONTENT_PATH' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 18
Notice: Use of undefined constant THEME_PATH - assumed 'THEME_PATH' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 19
Notice: Use of undefined constant STYLE_PATH - assumed 'STYLE_PATH' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 20
Notice: Use of undefined constant JS_PATH - assumed 'JS_PATH' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 21
Notice: Use of undefined constant JQ_PATH - assumed 'JQ_PATH' in C:'xampp'htdocs'task'ta-includes'class-settings.php on line 22

你的常量必须是字符串。所以你必须像这样用引号"包装"它们:

define('ROOT',$_SERVER['DOCUMENT_ROOT']);
       ^    ^

如有疑问,请始终检查手册,在这种情况下,请定义()

加:
我发现,在函数中定义常量也有点"狡猾"。如果您坚持不将它们移动到另一个(例如 config )文件,该文件在一开始就加载,然后至少将它们从类结构中删除。

我认为你误解了定义和常量是什么/从对象中以这种方式定义常量并没有意义。

也许这是您正在做的事情的更好方法:

http://php.net/manual/en/language.oop5.constants.php

或者,使用 define 设置项目的最常见用法是使用 si9mple 设置或配置文件...