2

I'm quite new to PHP Classes and I'm currently setting up a couple of Class Variables as shown below. The .time() function fails - third line below. If I remove the .time() it works.

class session {

  private $variable;
  private $cookieExpiry = time() + 15811200;      // Cookie Expire

  function __construct() {

Are you not allowed to set a variable like this with a default php function?

Is there a way to do this?

thankyou

1 Answer 1

4

The properties are a blueprint and must be independent of the runtime environment. You can set them to literal values but you can't make function calls like that. Set the initial properties in the constructor:

class session {

  private $variable;
  private $cookieExpiry;

  function __construct() {
      $this->cookieExpiry = time() + 15811200; // Cookie Expire
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 I'll give you this one, I think you beat me by like 5 seconds :)

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.