gpt4 book ai didi

php - 访问数组中的私有(private)值,从对象转换而来

转载 作者:可可西里 更新时间:2023-11-01 00:02:32 26 4
gpt4 key购买 nike

我有这个小代码:

class A
{
private $val = 5;
}

$a = new A();
$obj = (array)$a;
echo '<pre>'; var_dump ($obj); echo '</pre>';
echo $obj['Aval']; // error!

转储 $obj 后,结果是:

array(1) {
["Aval"]=>
int(5)
}

但是使用 $obj['Aval'] 访问这个值;触发错误 - 那是不可能的!

最佳答案

如果您查看 documentation on converting to an array ,它指出:

private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side.

这意味着它不是 A 放在前面,而是 \0A\0。所以关键是 "\0A\0val"

关于php - 访问数组中的私有(private)值,从对象转换而来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com