【问题标题】:PHP Use static variable in other variable in classPHP在类的其他变量中使用静态变量
【发布时间】:2017-02-08 11:46:38
【问题描述】:

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

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

像这样:

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

但我不断收到此错误:

致命错误:常量表达式包含无效操作 C:_server\htdocs\p\chestools\core\php\global.php 在第 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";

}

【问题讨论】:

标签: php class oop variables


【解决方案1】:

看来您使用的是低于 5.6 的 PHP 版本

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

5.6 之前的 PHP 版本

查看Property declarations 的 PHP 手册中的以下链接。

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

PHP 5.6

从 PHP 5.6 开始,您可以在 PHP 中声明默认类属性时使用串联。

【讨论】:

  • 上次我检查我的php版本是5.6.21。
  • 你能告诉我们你的代码第26行的内容是什么吗?
  • 公共静态 $path_home = "家";
  • 请查看这个答案,这是从类上下文中访问全局变量的一种可能形式。 stackoverflow.com/a/4489351/6366593
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
相关资源
最近更新 更多