数组对象和对象在PHP阵列 - 有趣的行为 [英] Array to Object and Object to Array in PHP - interesting behaviour
问题描述
你能解释下一个有趣的行为?
类的测试{
//类*测试*有两个属性,公共和私人。
公共$ xpublic ='X1';
私人$ xprivate ='X2';
}
$ testObj =新的测试();
让我们转换 $ testObj
数组。
的setType($ testObj,'阵');
后续代码var_dump($ testObj);
结果:
阵列(2){
[xpublic] =>串(3)X1
[testxprivate] =>串(4)X2
}
OK, xprivate
属性将变为 testxprivate
让我们转换这个数组对象。
$ newObj =(对象)$ testObj;
后续代码var_dump($ newObj);
结果:
对象(stdClass的)#1(2){
[xpublic] =>串(3)XXX
[xprivate:考:私人] =>串(4)XXX3
}
$ newObj
是 stdClass的
对象。
和问题是:
为什么 testxprivate
变成了私有财产 xprivate
(不是 testxprivate $ C新对象的$ C>)?如何PHP知道
$ testObj
数组是一个对象?
如果我定义的数组相等:
$ testArray =阵列('xpublic'=>'X1','testxprivate'=>'X2');
然后将其转换为对象:
的var_dump((对象)$ testArray);
我会得到的对象有两个公开性能 xpublic
和 testxprivate
作为预计:
对象(stdClass的)#2(2){
[xpublic] =>字符串(2)X1
[testxprivate] =>字符串(2)X2
}
数组键包含一个标记,这应该是类测试的私有财产。
具有以下比较你的脚本输出:
$阵列=阵列(
xpublic=> ×1,
#这将成为一个私有成员:
\\ x00test \\ x00xprivate=> ×2,
#这将成为一个受保护的成员:
\\ X00 * \\ x00xprotected=> X3
);后续代码var_dump($数组);$ OBJ =(对象)$数组;后续代码var_dump($ OBJ);
在系列化,相同的字符串用来描述私有成员。
输出:
阵列(3){
[xpublic] =>
串(2)×1
[testxprivate] =>
串(2)×2
[* xprotected] =>
字符串(2)X3
}对象(stdClass的)#1(3){
[xpublic] =>
串(2)×1
[xprivate:考:私人] =>
串(2)×2
[xprotected:保护] =>
字符串(2)X3
}
在的var_dump()
的输出,空字节是不可见的。
(更新:新增受保护的类成员)
Can you explain the next interesting behaviour?
class test {
//Class *test* has two properties, public and private.
public $xpublic = 'x1';
private $xprivate = 'x2';
}
$testObj = new test();
Let's convert $testObj
to array.
settype($testObj, 'array');
var_dump($testObj);
Result:
array(2) { ["xpublic"]=> string(3) "x1" ["testxprivate"]=> string(4) "x2" }
OK, xprivate
property becomes testxprivate
Let's convert this array to object.
$newObj = (object)$testObj;
var_dump($newObj);
Result:
object(stdClass)#1 (2) { ["xpublic"]=> string(3) "xxx" ["xprivate":"test":private]=> string(4) "xxx3" }
$newObj
is a stdClass
object.
And the question is:
Why does testxprivate
become a private property xprivate
(not testxprivate
) of the new object? How does PHP know that $testObj
array was an object?
If I define the equal array:
$testArray = array('xpublic'=>'x1', 'testxprivate'=>'x2');
and then convert it to object:
var_dump((object)$testArray);
I'll get the object with two public properties xpublic
and testxprivate
as expected:
object(stdClass)#2 (2) { ["xpublic"]=> string(2) "x1" ["testxprivate"]=> string(2) "x2" }
The array key contains a marker that this should be a private property of the class test.
Compare your scripts output with the following:
$array = array(
"xpublic" => "x1",
# this will become a private member:
"\x00test\x00xprivate" => "x2",
# this will become a protected member:
"\x00*\x00xprotected" => "x3"
);
var_dump($array);
$obj = (object) $array;
var_dump($obj);
When serialized, the same string is used to describe the private members.
Output:
array(3) { ["xpublic"]=> string(2) "x1" ["testxprivate"]=> string(2) "x2" ["*xprotected"]=> string(2) "x3" } object(stdClass)#1 (3) { ["xpublic"]=> string(2) "x1" ["xprivate":"test":private]=> string(2) "x2" ["xprotected":protected]=> string(2) "x3" }
In the output of var_dump()
, the null bytes are not visible.
(Update: Added protected class member)
这篇关于数组对象和对象在PHP阵列 - 有趣的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!