PHP 中的数组到对象和对象到数组 - 有趣的行为 [英] Array to Object and Object to Array in PHP - interesting behaviour

查看:48
本文介绍了PHP 中的数组到对象和对象到数组 - 有趣的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能解释下一个有趣的行为吗?

类测试{//Class *test* 有两个属性,public 和 private.公共 $xpublic = 'x1';私人 $xprivate = 'x2';}$testObj = 新测试();

让我们将 $testObj 转换为数组.

settype($testObj, 'array');var_dump($testObj);

结果:

数组(2){["xpublic"]=> 字符串(3) "x1"[testxprivate"]=> 字符串(4)x2"}

好的,xprivate 属性变成了 testxprivate

让我们将此数组转换为对象.

$newObj = (object)$testObj;var_dump($newObj);

结果:

object(stdClass)#1 (2) {["xpublic"]=> 字符串(3) "xxx"["xprivate":"test":private]=> string(4) "xxx3"}

$newObj 是一个 stdClass 对象.

问题是:

为什么 testxprivate 成为新对象的私有属性 xprivate(而不是 testxprivate)?PHP 如何知道 $testObj 数组是一个对象?

如果我定义相等数组:

$testArray = array('xpublic'=>'x1', 'testxprivate'=>'x2');

然后将其转换为对象:

var_dump((object)$testArray);

我将按预期获得具有两个 public 属性 xpublictestxprivate 的对象:

object(stdClass)#2 (2) {["xpublic"]=> 字符串(2) "x1"[testxprivate"]=> 字符串(2)x2"}

解决方案

数组键包含一个标记,表明这应该是类测试的私有属性.

将您的脚本输出与以下内容进行比较:

$array = array(xpublic" =>"x1",# 这将成为私人成员:"\x00test\x00xprivate" =>"x2",# 这将成为受保护的成员:"\x00*\x00xprotected" =>x3");var_dump($array);$obj = (object) $array;var_dump($obj);

序列化时,相同的字符串用于描述私有成员.

输出:

<前>数组(3){["xpublic"]=>字符串(2)x1"["testxprivate"]=>字符串(2)x2"["*xprotected"]=>字符串(2)x3"}对象(标准类)#1(3){["xpublic"]=>字符串(2)x1"["xprivate":"test":private]=>字符串(2)x2"["xprotected":protected]=>字符串(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆