0

This works fine:

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

But I need to make it more generic, to allow for different field names. So I try to use a constant:

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

But this does not target the correct array name, which should be [field_time][LANGUAGE_NONE][0]

I also tried:

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

But that throws: Parse error: syntax error, unexpected '['

What am I doing wrong?

6
  • 1
    "The value of the constant; only scalar and null values are allowed. Scalar values are integer, float, string or boolean values. It is possible to define resource constants, however it is not recommended and may cause unpredictable behavior. " Commented Jan 6, 2014 at 12:37
  • @Flosculus He did not want to use constant as array, but it's value. Let's say you have a class with property(array) = public $_arr = array(...); and in another file define('BLA', '_arr');. OP wants to do $obj->_arr = ... but using constant value => $obj->BLA = Commented Jan 6, 2014 at 12:40
  • If FIELD_TIME is a constant of the class, shouldn't it be accessed statically like EntityClass::FIELD_TIME? Commented Jan 6, 2014 at 12:42
  • @Flosculus It's a constant of a file than instantiates the class. You have class Entity { public $field_time = null; } $entity = new Entity(); define('FIELD_TIME', 'field_time'); Commented Jan 6, 2014 at 12:45
  • Ah i get it. It seems a little off however. If the properties of the class need to be created dynamically, wouldn't something akin to Doctrine's ArrayCollection or even old fashioned magic be more suitable? Commented Jan 6, 2014 at 12:48

4 Answers 4

1

Try this

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

Yes, just bracket the constant ! This also work for function calls

$entity->{FUNC_NAME_CONST}();
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot access an array directly from the returnvalue of a method.

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

is wrong, store the return value of constant() first in another variable and access LANGUAGE_NONE via that array.

Comments

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

This way should work, but as of PHP 5.4

Otherwise you will need one more line:

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

Function array dereferencing has been added, e.g. foo()[0].


If I understood correctly, it should be used as:

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');

Comments

0

if i understand you good, you just want use the constant to call the correct array by its name?

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

this works fine for me if you have a declared array with the name "field_time"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.