0

I have a minor problem with using a global variable class for my PHP app. I really want to use this method of storing variables because I know I'll be changing file names and folder structures as time goes on.

I want to use the variable public $rootin the variables under it, that aren't in a method.

Like this:

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

But I keep getting this error:

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

Here's my full page:

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

1 Answer 1

0

This seems you are using a PHP version less than the 5.6

If you want really concatenate content in a property initialization, you can do it through the __construct() function

PHP Versions Before 5.6

Check out the following link in the PHP Manual for 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 onwards, you can use concatenation when declaring default class properties in PHP.

Sign up to request clarification or add additional context in comments.

4 Comments

last time i checked my php version, it was 5.6.21.
Can you please tell us what is the content of the line 26 of your code?
public static $path_home = "Home";
Please check out this answer, this is a possible form to access global variables from within class context. stackoverflow.com/a/4489351/6366593

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.