2

I want a dynamic property name like this:

$_taxVocabName = 'name';
$node->field_ . $_taxVocabName;

This should call:

$node->field_name

How can I do this?

I could not find anything on php.net or elsewhere.

Thanks Sascha

Michael
  • 11,912
  • 6
  • 49
  • 64
Sascha Heim
  • 301
  • 1
  • 4
  • 15

2 Answers2

5

Two ways:

First, use a variable:

$property = 'field_' . $_taxVocabName;

$node->$property;

Second, use curly brackets:

$node->{'field_' . $_taxVocabName};
Michael
  • 11,912
  • 6
  • 49
  • 64
1

I'd think a much better way to do this would be

$taxVocabName = "name";
$property = "field_" . $taxVocabName;

Then you would have to set the variable to a value of course...

like so:

$$property = "My variable variable";

or:

$field_name = "My variable variable";

I know it's a bit later, but maybe this will help someone else down the road.

Highspeed
  • 442
  • 3
  • 18