简体   繁体   English

PHP对象属性表示法

[英]PHP object property notation

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. dsm()是Drupal开发人员函数,用于调试打印变量,对象和数组。

Why $source object doesn't understand $obj->{$variable} notation? 为什么$source对象不理解$obj->{$variable}表示法? Notice: Undefined property: stdClass::$field_phone['und']['0']['value'] 注意:未定义属性:stdClass :: $ field_phone ['und'] ['0'] ['value']

Because your object does not have a property that is named " field_phone['und'][0]['value'] " . 因为您的对象没有名为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. 它有一个名为field_phone的属性,它是一个具有名为und的索引的数组,它是一个索引为0的数组,依此类推。 But the notation $obj->{$var} does not parse and recursively resolve the name, as it shouldn't. 但符号$obj->{$var}不会解析并递归解析名称,因为它不应该。 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. 这不像复制和粘贴源代码代替$var那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM