duanmianhong4893 2014-10-10 11:37
浏览 34
已采纳

如何访问对象的数字属性

When I run this bit

$data = (object)array(1,2,3);
print_r($data);

I get

stdClass Object
(
    [0] => 1
    [1] => 2
    [2] => 3
)

This is rather interesting because I've known up until now that an object's property name cannot start with a number. So if that is true it must mean that these values are not properties, but what are they then?

A few ways I tried to access the values

$data[0]; // Fatal error:  Cannot use object of type stdClass as array
$data->0; // Parse error:  syntax error, unexpected 0, expecting identifier
$data->{0}; // Notice:  Undefined property: stdClass::$0

I'm not really interested in accessing the values rather than finding out how they are kept in the class if they're not properties and not indexed values.

  • 写回答

3条回答 默认 最新

  • doushao5047 2014-10-10 11:52
    关注

    I'm not really interested in accessing the values rather than finding out how they are kept in the class if they're not properties and not indexed values.

    That means diving into PHP´s internals, which is probaly not where you want to find yourself. You'd have to check PHP's own source code to see what exactly is happening in these cases.

    Is there a specific reason you want to know?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题