json - Set a variable to what json_encode() outputs in PHP -


json_encode() returns string...

so shouldn't following code work?

class testclass {     private $test = json_encode("test"); } 

php outputs

parse error: syntax error, unexpected '(', expecting ',' or ';' in /home/testuser/public_html/test.php on line 10 

you can not assign expression or variable while declaring class property. literal constants such __file__ allowed here.

they have literal value, such string, or constant.

there work.

private $test= 98; private $test= "test value"; private $test= constant; private $test= __file__; 

but these not

private $test= 98*2; private $test= "test value"."some other value"; 

you can use constructor

 function __construct() {         $this->test = json_encode("test");     } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -