tags:

views:

30

answers:

2

Hey there. I've got the following code:

class user {

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

  ...
  ...
  ...

And i get

and unexpected T_VARIABLE error.

Can somebody tell me how to construct that url so it won't give me an error?

+2  A: 

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

class a
{
 private $signInURL;
 public function __construct()
 {
  $this->signInURL = $_SERVER['DOCUMENT_ROOT'].'/?page=signin';
 }
}
Kai Sellgren
You cannot use $this-> there because it's a static property. Also this will set the variable every time that class is created which might not be the intended behaviour (and the variable might be accessed without ever having created an instance of that class, i.e. by other static methods)
dbemerlin
+2  A: 

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;
  }
}
dbemerlin
yes. your suggestion works ok and is an elegant solution as well. thanks for your help
Illes Peter