dongzhou4727 2011-04-18 08:17
浏览 39
已采纳

PHP StdClass - 访问字段为 - >字段或 - > {'字段'}

I'm working on this PHP code collaboration and it seems that some people access PHP StdClass fields like this

$body->head

and others like

$body->{'head'}

As far as I can tell these are equivalent. Are they? Does it matter which is used? Which way would you prefer? Any quirks to look out for here?

  • 写回答

2条回答 默认 最新

  • dtrpv60860 2011-04-18 08:22
    关注

    They are equivalent. You only need the second version if you want to use abhorrent attribute names like:

    $data->{'what * the ! thing'}
    

    This sometimes happens if you convert pure data arrays into objects.

    But there is also the double quotes version, which makes a bit more sense when you actually need variable attribute names (basically variable variables for objects):

    $data->{"attr$index"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀