dsewbh5588 2009-12-08 21:01
浏览 56
已采纳

使用数字键作为对象转换数组

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)

  • 写回答

4条回答 默认 最新

  • douou9786 2009-12-08 21:29
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢