你能解释下一个有趣的行为吗?
class test {
//Class *test* has two properties, public and private.
public $xpublic = 'x1';
private $xprivate = 'x2';
}
$testObj = new test();
Run Code Online (Sandbox Code Playgroud)
让我们转换$testObj为数组.
settype($testObj, 'array');
var_dump($testObj);
Run Code Online (Sandbox Code Playgroud)
结果:
array(2) {
["xpublic"]=> string(3) "x1"
["testxprivate"]=> string(4) "x2"
}
好的,xprivate财产变成了testxprivate
让我们将这个数组转换为object.
$newObj = (object)$testObj;
var_dump($newObj);
Run Code Online (Sandbox Code Playgroud)
结果:
object(stdClass)#1 (2) {
["xpublic"]=> string(3) "xxx"
["xprivate":"test":private]=> string(4) "xxx3"
}
$newObj是一个stdClass对象.
问题是:
为什么testxprivate成为新对象的私有财产xprivate(而不是testxprivate)?PHP如何知道该$testObj数组是一个对象?
如果我定义相等的数组:
$testArray = array('xpublic'=>'x1', 'testxprivate'=>'x2'); …Run Code Online (Sandbox Code Playgroud) 我不知道发生了什么,但我在数组中有一个字符串.它必须是一个字符串,因为我先在它上面运行它:
$array[0] = (string)$array[0];
Run Code Online (Sandbox Code Playgroud)
如果我以纯文本形式将$ array [0]输出到浏览器,它会显示:
hellothere
Run Code Online (Sandbox Code Playgroud)
但如果我JSON编码$ array我得到这个:
hello\u0000there
Run Code Online (Sandbox Code Playgroud)
另外,我需要将'there'部分(\ u0000之后的位)分开,但这不起作用:
explode('\u0000', $array[0]);
Run Code Online (Sandbox Code Playgroud)
我甚至不知道\ u0000是什么或如何在PHP中控制它.
我确实看到了这个链接:试图从我的json中找到并摆脱这个\ u0000 ...这表明str_replacing生成的JSON.我不能这样做(并且需要先将上面提到的它分开)所以我然后检查了Google的'php check for backslash\0 byte'但我仍然无法解决该怎么做.