简体   繁体   English

PHP动态变量名称

[英]PHP Dynamic Variable Name

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. 我在php.net或其他地方找不到任何内容。

Thanks Sascha 谢谢萨莎

Two ways: 两种方式:

First, use a variable: 首先,使用一个变量:

$property = 'field_' . $_taxVocabName;

$node->$property;

Second, use curly brackets: 其次,使用大括号:

$node->{'field_' . $_taxVocabName};

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. 我知道会晚一点,但是也许这会帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM