5

Is is possible to concatenate an object's name? The below doesn't seem to work..

Trying to call $node->field_presenter_en;

$lang = 'en';

$node->field_presenter_.$lang;

${$node->field_presenter_.$lang};

Thanks!

3 Answers 3

20

Try:

$field_presenter = 'field_presenter_'.$lang;
$node->$field_presenter;

This is called variable variables. More information here: http://php.net/manual/en/language.variables.variable.php

Edit: The user nickb has suggested a much more elegant solution below, and I will incorporate into this answer for easier reading (nickb: please let me know if you want me to remove this):

$node->{'field_presenter_'.$lang}
1
  • 1
    nickb's $node->{'field_presenter_' . $lang} seems to be the best answer but I guess Ynhockey is also correct! thanks
    – Pedro
    Commented Feb 2, 2013 at 17:58
3
$field_presenter = 'field_presenter_'.$lang;
$node->$field_presenter;
1
  • Was trying to improve Ynhockey's answer but we edited at the same time.
    – mimipc
    Commented Feb 2, 2013 at 17:54
2
<?php
class A {
    public $prop = 'hello';
}

$a = new A();
echo $a->{'pro' . 'p'}; // hello

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.