Pedro
Pedro

Reputation: 208

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!

Upvotes: 5

Views: 7987

Answers (3)

Ynhockey
Ynhockey

Reputation: 3932

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}

Upvotes: 20

Eugene Naydenov
Eugene Naydenov

Reputation: 7295

<?php
class A {
    public $prop = 'hello';
}

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

Upvotes: 2

mimipc
mimipc

Reputation: 1374

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

Upvotes: 3

Related Questions