Php unexpected T_VARIABLE, little help here

Hey there. I have the following code:

class user {

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

  ...
  ...
  ...

      

And i get

and an unexpected error T_VARIABLE.

Can someone tell me how to build this url so that it doesn't give me an error?

+2


a source to share


2 answers


You cannot use a variable there, you have to transfer it to a method. This is bad style, as the User class needs to know about $ _SERVER.

If you really want this, you can 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;
  }
}

      

+4


a source


You cannot put variables as the value of class properties. Try,



class a
{
 private $signInURL;
 public function __construct()
 {
  $this->signInURL = $_SERVER['DOCUMENT_ROOT'].'/?page=signin';
 }
}

      

+1


a source







All Articles