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

3

2 Answers 2

5

Two ways:

First, use a variable:

$property = 'field_' . $_taxVocabName;

$node->$property;

Second, use curly brackets:

$node->{'field_' . $_taxVocabName};
Sign up to request clarification or add additional context in comments.

Comments

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.

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.