开发者

php unexpected T_VARIABLE, little help here

开发者 https://www.devze.com 2022-12-28 07:15 出处:网络
Hey 开发者_开发知识库there. I\'ve got the following code: class user { //URLs private static $signInURL = $_SERVER[\'DOCUMENT_ROOT\'].\'/?page=signin\';

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

精彩评论

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