Search code examples
phpsyntax-error

PHP reports: unexpected T_VARIABLE


I've got the following code:

class user {
            
  //URLs
  private static $signInURL = $_SERVER['DOCUMENT_ROOT'].'/?page=signin';
        
  ...
  ...
  ...

And I get an unexpected T_VARIABLE error.

How can I construct that url so it won't give me an error?


Solution

  • You cannot use a variable there, you should move it into a method. It's bad style anyways as the class User has to know about $_SERVER.

    If you really, really want it that way you could use:

    private static $signInURL = '';
    
    public static getSignInUrl()
    {
      if (User::$signInUrl == '') User::$signInUrl = $_SERVER....;
      return User::$signInUrl;
    }
    

    I suggest using:

    class User
    {
      private static $signInUrl = '/signin';
    
      public static getSignInUrl($base)
      {
        return $base . User::$signInUrl;
      }
    }