将变量设置为PHP中json_encode()输出的内容 [英] Set a variable to what json_encode() outputs in PHP
本文介绍了将变量设置为PHP中json_encode()输出的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
json_encode()返回一个字符串...
json_encode() returns a string...
下面的代码不行吗?
class TestClass {
private $test = json_encode("test");
}
PHP输出
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home/testuser/public_html/test.php on line 10
推荐答案
在声明类属性时不能分配表达式或任何变量.此处允许使用文字常量,例如__FILE__
.
You can not assign expression or any variable while declaring class property. The literal constants such as __FILE__
are allowed here.
它们必须是文字值,例如字符串或常量.
they have to be a literal value, such as a string, or a constant.
所有工作.
private $test= 98;
private $test= "test value";
private $test= CONSTANT;
private $test= __FILE__;
但是这些不会
private $test= 98*2;
private $test= "test value"."some other value";
您可以使用构造函数
function __construct() {
$this->test = json_encode("test");
}
这篇关于将变量设置为PHP中json_encode()输出的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文