2

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?

1
  • no it's not, edited so it more readable Commented Apr 22, 2010 at 13:51

2 Answers 2

4

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;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes. your suggestion works ok and is an elegant solution as well. thanks for your help
1

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

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.