开发者

php unexpected T_VARIABLE, little help here

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?


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;
  }
}


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';
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜