使用数字键作为对象来投射数组 [英] Casting an Array with Numeric Keys as an Object

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

问题描述

  $ o 

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

根据我的理解,PHP属性需要使用与PHP变量相同的规则来声明。即有效的变量名称以字母或下划线开头,后跟任意字母,数字或下划线的数量。但是,上面的代码产生以下输出

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

真的很奇怪,当你尝试访问对象中的信息。

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

有没有得到var_dump报告的信息在对象中,还是只是锁定了其余的请求生命周期? (实际使用这是零,我只是好奇)

解决方案

是的,他们只是被锁定,除非回数组。在PHP中有一些Gotchas,例如在旧版本中,你可以定义一个常量作为数组,但是从不访问它的元素。即使现在你可以定义一个常量作为资源(例如, define('MYSQL',mysql_connect()); ),虽然这导致相当不可预测的行为,避免。



通常,最好避免数组到对象转换,如果可能的话。如果你真的需要这样做,考虑创建一个 stdClass 的新实例,然后手动重命名所有的变量,例如到 _0 _1 等。

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

编辑:刚刚对这个假设做了一个快速测试,是的,他们在对象上下文中正式不存在数据被存储,但是它不可能访问,因此是最终的私人成员。这里是测试:

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

输出为:

  bool(false)
bool(false)



AGAIN:有趣的旁注,以下操作返回false:

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

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

var_dump($ y == $ z);


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);

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) { }

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)

解决方案

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.

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;
}

EDIT: 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'));

And the output is:

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天全站免登陆