PHP在类中的其他变量中使用静态变量

时间:2016-09-30 01:29:28

标签: php class oop variables

我的PHP应用程序使用全局变量类有一个小问题。我真的想使用这种存储变量的方法,因为我知道随着时间的推移,我将改变文件名和文件夹结构。

我想在其下面的变量中使用变量public $root,而不是在方法中。

像这样:

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

但我一直收到这个错误:

  

致命错误:常量表达式包含无效操作   C:第26行的_server \ htdocs \ p \ chestools \ core \ php \ global.php

这是我的整页:

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";

}

1 个答案:

答案 0 :(得分:0)

这似乎是您使用的PHP版本低于5.6

如果你想在属性初始化中真正连接内容,可以通过__construct()函数

来实现。

5.6版本之前的PHP版本

查看PHP手册中Property declarations

的以下链接
  

......这个声明   可能包括初始化,但这个初始化必须是a   常量值 - 也就是说,它必须能够在编译时进行评估   时间并且不得依赖于运行时信息   评价。

PHP 5.6

PHP 5.6以后,您可以在PHP中声明默认类属性时使用连接。