php - date() inside class causes an error -


this no doubt super dumb question, im getting grips classes , im trying assign variable, if use string variable value works fine when use:

class mytest{     private $my_date = date("y-m-d h:i:s"); }  $my_test = new mytest; echo $my_test->$my_date; 

then server error doesnt print log saying whats wrong, dreamviewer shows error if load outside of class fine.

no laughing please, doing wrong?

if this

class mytest{     private $my_date = "today"; }  $my_test = new mytest; echo $my_test->$my_date; 

then no error.

you can't call functions when declaring class variables. need set value inside class constructor. this:

class mytest{     private $my_date;      public function __construct() {         $this->my_date = date("y-m-d h:i:s");     } } 

see here: http://www.php.net/manual/en/language.oop5.properties.php

class member declaration "may include initialization, initialization must constant value--that is, must able evaluated @ compile time , must not depend on run-time information in order evaluated."


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -