热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHP中的数组到对象和对象到数组-有趣的行为-ArraytoObjectandObjecttoArrayinPHP-interestingbehaviour

Canyouexplainthenextinterestingbehaviour?你能解释下一个有趣的行为吗?classtest{Class*test*hastw

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.

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

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

Result:

结果:

array(2) {
  ["xpublic"]=> string(3) "x1"
  ["testxprivate"]=> string(4) "x2"
}

OK, xprivate property becomes testxprivate

好的,xprivate属性变为testxprivate

Let's convert this array to object.

让我们将这个数组转换为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.

$ newObj是一个stdClass对象。

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?

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

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:

我将按预期获得具有两个公共属性xpublic和testxprivate的对象:

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

2 个解决方案

#1


19  

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.

在var_dump()的输出中,空字节不可见。

(Update: Added protected class member)

(更新:添加受保护的类成员)

#2


0  

Probably the PHP engine conserves the class structure internaly and simply gives some kind of an array wrapper, and thus when you cast it again it remains private, though I can't assure this at 100%.

可能PHP引擎保留了类结构internaly并简单地给出了某种类型的数组包装器,因此当你再次转换它时它仍然是私有的,尽管我不能保证100%。


推荐阅读
author-avatar
细野本尊
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有