silkfire
silkfire

Reputation: 26033

Static variable issue

I have this static variable that I'm defining but I get an error in my code:

..unexpected '$_SERVER' (T_VARIABLE) in ...

class Constants {
        const ACCOUNTTYPE_SUPER_ADMIN   = 1;
        const ACCOUNTTYPE_COMPANY_ADMIN = 2;
        const ACCOUNTTYPE_AREA_ADMIN    = 3;
        const ACCOUNTTYPE_END_USER      = 4;

        const SAVETYPE_NEW              = 0;
        const SAVETYPE_EDIT             = 1;

        const LICENSE_VALIDITY_YEARS    = 1;
        const LICENSE_VALIDITY_LEFT_MAX = 12;

        public static $template_path = $_SERVER['DOCUMENT_ROOT'] . '/../_html/';
}

Upvotes: 2

Views: 591

Answers (3)

ungalcrys
ungalcrys

Reputation: 5620

you can use a static function

class Constants {
    // ...
    public static function getTemplatePath()
    {
        return $_SERVER['DOCUMENT_ROOT'] . '/../_html/';
    }
}

and can be used like

Constants::getTemplatePath();

Upvotes: 0

Frhay
Frhay

Reputation: 424

You cannot declare a static variable using a variable that way, but you can use a workaround for this:

class Constants {
    ...

    public static $template_path;
}

Constants::$template_path = $_SERVER['DOCUMENT_ROOT'] . '/../_html/';

Upvotes: 3

Glorious Kale
Glorious Kale

Reputation: 1313

You can only assign direct values when defining class members.

But you can create a method init() that would change your template path member value.

  public static function init(){ self::$template_path = $_SERVER['DOCUMENT_ROOT'] . '/../_html/'; }

and run it when you first use the class or instantiate it.

Upvotes: 2

Related Questions