铸造用数字键数组作为一个对象 [英] Casting an Array with Numeric Keys as an Object

查看:87
本文介绍了铸造用数字键数组作为一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我四处PHP的铸造机制,跑进一个奇怪的情况下,铸造数组作为对象时

I was poking around PHPs casting mechanism, and ran into an odd case when casting an array as an object

$o = (object) array('1'=>'/foo/bar');	
$o = new stdClass();
var_dump($o);

据我了解,PHP性能需要使用相同的规则PHP变量声明。这是一个有效的变量名以字母或下划线开始,后面跟着任何数量字母,数字或下划线。然而,上述code产生以下输出

As I understand it, PHP properties need to be declared with the same rules as PHP variables. That is A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. However, the above code produces the following output

object(stdClass)#1 (1) {
  [1]=>
  string(8) "/foo/bar"
}

如果它变得非常奇怪是当您尝试访问的对象的信息。

Where it gets really weird is when you try to access that information in the object.

var_dump($o->1); 		// parse error
var_dump($o->{'1'}); 		// NULL	
var_dump(get_object_vars($o));  //array(0) { }

反正是有得到的是报告的var_dump是对象,或只是锁定为请求生命周期的其他信息? (这实际使用中是零,我只是好奇)

Is there anyway to get at the the information that var_dump reports is in the object, or is it just locked up for the rest of the request life cycle? (practical use of this is nil, I'm just curious)

推荐答案

是的,他们只是被锁,除非施法回一个数组。 PHP中的几个小陷阱,例如在老版本,你可以定义常量为一个数组,但从来没有访问它的元素。即使是现在,你可以定义一个常数作为资源(例如,定义('MYSQL',mysql_connect()函数); ),尽​​管这会导致相当未predictable behavoir和再次,应避免使用。

Yes, they are just locked away unless cast back to an array. There are a few little "Gotchas" in PHP, for example in older versions you could define a constant as an array, but then never access its elements. Even now you can define a constant as a resource (e.g., define('MYSQL',mysql_connect());) although this leads to rather unpredictable behavoir and, again, should be avoided.

一般情况下,最好避免阵列到目标施放,如果在所有可能的。如果你真的需要做到这一点,可以考虑手动创建 stdClass的的新实例,然后重命名所有的变量,例如 _0 _1

Generally, it's best to avoid array-to-object casts if at all possible. If you really need to do this, consider creating a new instance of stdClass and then manually renaming all the variables, for example to _0, _1, etc.

$a = array('cat','dog','pheasant');
$o = new stdClass;
foreach ($a as $k => $v) {
    if (is_numeric($k)) {
        $k = "_{$k}";
    }
    $o->$k = $v;
}

编辑:只是做了这个假设多了一个快速测试,是的,他们正式不存在,在对象范围内;数据存储,但它是不可能的访问,因此,是最终的私有成员。这里是测试

Just did one more quick test on this hypothesis, and yes, they officially "do not exist" in object context; the data is stored, but it's impossible to access, and is therefore the ultimate private member. Here is the test:

$a = array('one','two','three');
$o = (object)$a;
var_dump(property_exists($o, 1), property_exists($o, '1'));

和输出是:

bool(false)
bool(false)

再次编辑:有趣的边注,下面的操作回来假的:

EDIT AGAIN: Interesting side-note, the following operation comes back false:

$a = array('one','two','three','banana' => 'lime');
$b = array('one','two','banana' => 'lime');

$y = (object)$a;
$z = (object)$b;

var_dump($y == $z);

这篇关于铸造用数字键数组作为一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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