【问题标题】:PHP newbie: can't use a constant to define arrayPHP新手:不能使用常量来定义数组
【发布时间】:2014-01-06 12:34:03
【问题描述】:

这很好用:

$entity->field_time[LANGUAGE_NONE][0] = array(
  'value' => date_format($date),
  'timezone' => 'UTC',
  );

但我需要使其更通用,以允许使用不同的字段名称。所以我尝试使用一个常量:

define('FIELD_TIME', 'field_time');
$entity->FIELD_TIME[LANGUAGE_NONE][0] = array(
  'value' => date_format($date),
  'timezone' => 'UTC',
  );

但这并没有针对正确的数组名称,应该是[field_time][LANGUAGE_NONE][0]

我也试过了:

define('FIELD_TIME', 'field_time');
$entity->constant('FIELD_TIME')[LANGUAGE_NONE][0] = array(
  'value' => date_format($date),
  'timezone' => 'UTC',
   );

但这会抛出:解析错误:语法错误,意外'['

我做错了什么?

【问题讨论】:

  • "常量的值;只允许标量和空值。标量值是整数、浮点数、字符串或布尔值。可以定义资源常量,但不建议这样做,可能导致不可预知的行为。"
  • @Flosculus 他不想使用常量作为数组,但它是值。假设您有一个属性(数组)=public $_arr = array(...); 的类,并且在另一个文件中define('BLA', '_arr');。 OP 想做 $obj->_arr = ... 但使用常量值 => $obj->BLA =
  • 如果FIELD_TIME是类的常量,不应该像EntityClass::FIELD_TIME一样静态访问吗?
  • @Flosculus 它是一个文件的常量,而不是实例化类。你有class Entity { public $field_time = null; } $entity = new Entity(); define('FIELD_TIME', 'field_time');
  • 啊,我明白了。不过好像有点偏了。如果类的属性需要动态创建,那么类似 Doctrine 的 ArrayCollection 甚至老式的魔法不是更合适吗?

标签: php arrays constants


【解决方案1】:

试试这个

$entity->{FIELD_TIME}[LANGUAGE_NONE][0] = 'something';

是的,只需将常量括起来!这也适用于函数调用

$entity->{FUNC_NAME_CONST}();

【讨论】:

    【解决方案2】:

    您不能直接从方法的返回值访问数组。

    $entity->constant('FIELD_TIME')[LANGUAGE_NONE][0]
    

    错了,先把constant()的返回值存入另一个变量,然后通过那个数组访问LANGUAGE_NONE。

    【讨论】:

      【解决方案3】:
      define('FIELD_TIME', 'field_time');
      $entity->constant('FIELD_TIME')[LANGUAGE_NONE][0] = array(
        'value' => date_format($date),
        'timezone' => 'UTC',
         );
      

      这种方式应该可行,但从 PHP 5.4 开始

      否则你将需要多一行:

      define('FIELD_TIME', 'field_time');
      $field_time = constant('FIELD_TIME');
      $entity->$field_time[LANGUAGE_NONE][0] = array(
      'value' => date_format($date),
            'timezone' => 'UTC',
             );
      

      http://php.net/manual/en/migration54.new-features.php

      已添加函数数组解引用,例如foo()[0].


      如果我理解正确,它应该被用作:

      class Entity {
          public $field_time = null;
      }
      $entity = new Entity();
      /** 
      * Instead of:
      * $entity->field_time[LANGUAGE_NONE][0] = array('key' => 'val');
      */
      define('FIELD_TIME', 'field_time');
      $field_time = constant('FIELD_TIME');
      // or $field_time = FIELD_TIME;
      $entity->$field_time[LANGUAGE_NONE][0] = array('key' => 'val');
      

      【讨论】:

        【解决方案4】:

        如果我对你的理解很好,你只想使用常量通过它的名称调用正确的数组吗?

        define('FIELD_TIME', 'field_time');
        $entity->{FIELD_TIME}[LANGUAGE_NONE][0] = array(
          'value' => date_format($date),
          'timezone' => 'UTC',
          );
        

        如果你有一个名为“field_time”的声明数组,这对我来说很好

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-17
          • 1970-01-01
          • 2011-09-29
          • 2016-08-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多