PHP对象属性符号 [英] PHP object property notation

查看:175
本文介绍了PHP对象属性符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我突然停在这里:

  $ source =(object)array(
'field_phone'=> array(
'und'=> array(
'0'=> array(
'value'=>'000-555-55-55',
),
),
),
);

dsm($ source);
$ source_field =field_phone ['und'] [0] ['value'];
dsm($ source-> {$ source_field}); //这个符号不起作用
dsm($ source-> field_phone ['und'] [0] ['value']); //这是




  • dsm() / code>是用于调试打印变量,对象和数组的Drupal开发人员功能。



为什么 $源对象不明白 $ obj-> {$ variable} 符号?
注意:未定义的属性:stdClass :: $ field_phone ['und'] ['0'] ['value']

解决方案>

因为您的对象没有名为的属性 field_phone ['und'] [0] ['value'] 。它有一个名为 field_phone 的属性,它是一个数组,它的索引名为 und 这是一个数组,它有一个索引 0 等等。但是,符号 $ obj-> {$ var} 不会解析并递归地解析名称,因为它不应该。它只是在给定对象上查找给定名称的属性,没有更多。它不像复制和粘贴源代码代替 $ var 那里。


I suddenly stuck here:

  $source = (object) array(
      'field_phone' => array(
          'und' => array(
              '0' => array(
                  'value' => '000-555-55-55',
              ),
          ),
      ),
  );

  dsm($source);
  $source_field = "field_phone['und'][0]['value']";
  dsm($source->{$source_field});                    //This notation doesn't work
  dsm($source->field_phone['und'][0]['value']);     //This does

  • dsm() is Drupal developer function for debug printing variables, objects and arrays.

Why $source object doesn't understand $obj->{$variable} notation? Notice: Undefined property: stdClass::$field_phone['und']['0']['value']

解决方案

Because your object does not have a property that is named "field_phone['und'][0]['value']". It has a property that is named "field_phone" which is an array which has an index named "und" which is an array which has an index 0 and so on. But the notation $obj->{$var} does not parse and recursively resolve the name, as it shouldn't. It just looks for the property of the given name on the given object, nothing more. It's not like copy and pasting source code in place of $var there.

这篇关于PHP对象属性符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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