Question

Is the only way to assign $systime a value of a built-in-functions, is through a method?

class Test{
    private  $systime;
    public function get_systime(){
       $this->systime = time();
    }
}

Right off i would think something like this right?:

class Test{
    private  $systime = time();
    public function get_systime(){
      echo $this->systime;
    }
}

Thanks

Was it helpful?

Solution

You should be able to use a constructor to assign the value, for example:

class Test {
  private $systime;
  function __construct() {
    $this->systime = time();
  }

  public function get_systime(){
    echo $this->systime;
  }
}


$t = new Test();
$t->get_systime();

For more information on __construct() see the php manual section on object oriented php.

OTHER TIPS

From http://www.php.net/manual/en/language.oop5.basic.php (Just before Example 3)

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

However, you can also assign a value from the constructor:

class Test{
    private  $systime;
    public function __construct(){
        $this->systime = time();
    }
    public function get_systime(){
      echo $this->systime;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top