tags:

views:

82

answers:

3
+1  Q: 

Object properties

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

+2  A: 

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.

zimbu668
+2  A: 

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;
    }
}
Jordan S. Jones
A: 

I thank both of you guys.

Babiker
I think you were looking for the "add comment" box. The text box that is titled "Your Answer:" is for posting answers to questions.
Calvin