2016-09-30 210 views
0

我對我的PHP應用程序使用全局變量類有一個小問題。我真的想要使用這種存儲變量的方法,因爲我知道隨着時間的推移我會改變文件名和文件夾結構。PHP在類中使用其他變量的靜態變量

我想在它下面的變量中使用變量public $root,它們不在方法中。

像這樣:

public $root = "/rootfolder/"; 
public $something = $root . "path/"; 

但我不斷收到此錯誤:

Fatal error: Constant expression contains invalid operations in C:_server\htdocs\p\chestools\core\php\global.php on line 26

這裏是我的整頁:

class gvars { 

    public static $debug_mode = true; 

    /* 
    * Tab Title Control 
    * n\a 
    */ 
    public static $tab_after    = " | Chestools"; 
    public static $tab_name_enable   = true; 

    /* 
    * Dialouge Control 
    * utils.dialouges.php 
    */ 
    public static $dia_show_fatal = true; 
    public static $dia_enabled = true; 

    /* 
    * Page Paths 
    * n\a 
    */ 
    public static $root      = "/p/chestools/"; 
    public static $path_home    = "Home"; 
    public static $path_contactus   = "Contactus"; 
    public static $path_login    = "Login"; 
    public static $path_signup    = "Signup"; 
    //General Pages 
    public static $path_help    = "Help"; 
    public static $path_profile    = "Profile"; 
    //Student Pages 
    public static $path_select_cat   = "Catagorys"; 
    //Teacher Pages 
    public static $path_view_student  = "View"; 
    public static $path_create    = "Create"; 
    //Catagorys 
    public static $path_cat_math   = "c/math/"; 
    public static $path_cat_life_sci  = "c/life-science/"; 
    public static $path_cat_int_sci   = "c/interactive-science/"; 
    public static $path_cat_earth_sci  = "c/earth-science/"; 
    //Security 
    public static $path_sec_finish   = "security/Finish"; 
    public static $path_sec_change_password = "security/ChangePassword"; 
    public static $path_sec_change_email = "security/ChangeEmail"; 
    public static $path_sec_checkpoint  = "security/Checkpoint"; 

    /* 
    * System Paths 
    * n\a 
    */ 
    public static $head_css     = "core/css/chestools.css"; 
    public static $head_js_app    = "core/js/app.js"; 

    /* 
    * Footer Resource Paths 
    * n\a 
    */ 
    public static $path_dwa     = "about/district-wide-accounts/"; 
    public static $path_isa     = "about/independent-school-   accounts/"; 
    public static $path_support    = "support/"; 
    public static $path_pp     = "Privacy"; 
    public static $path_tos     = "Terms"; 

} 
+0

無法在[5.0.0 - 5.6.26,hhvm-3.10.0 - 3.14.4,7.0.0 - 7.1.0RC3](https://3v4l.org/88Lot#v500) – Gordon

回答

0

這似乎是您使用的PHP版本小於5.6

如果你想真正串聯在屬性初始化的內容,你可以做到這一點通過__construct()功能

PHP版本之前5.6

退房PHP手冊在以下鏈接Property declarations

...This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

PHP 5.6

PHP 5.6起,您可以在PHP聲明默認的類屬性時使用串聯。

+0

上次重現檢查我的PHP版本,它是5.6.21。 –

+0

你能告訴我們你的代碼第26行的內容是什麼嗎? – Anass

+0

public static $ path_home =「Home」; –