Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Concatenate object class name

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!

like image 248
Pedro Avatar asked Feb 02 '13 17:02

Pedro


2 Answers

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}
like image 185
Ynhockey Avatar answered Oct 10 '22 08:10

Ynhockey


$field_presenter = 'field_presenter_'.$lang;
$node->$field_presenter;
like image 28
mimipc Avatar answered Oct 10 '22 08:10

mimipc