[英]PHP: Variable in a function name
I want to trigger a function based on a variable. 我想基于变量触发一个函数。
function sound_dog() { return 'woof'; } function sound_cow() { return 'moo'; } $animal = 'cow'; print sound_{$animal}(); *
The * line is the line that's not correct. *行是不正确的行。
I've done this before, but I can't find it. 我以前做过这个,但我找不到它。 I'm aware of the potential security problems, etc.
我知道潜在的安全问题等。
Anyone? 任何人? Many thanks.
非常感谢。
You can do that, but not without interpolating the string first: 你可以这样做,但不能没有先插入字符串:
$animfunc = 'sound_' . $animal;
print $animfunc();
Or, skip the temporary variable with call_user_func() : 或者,使用call_user_func()跳过临时变量:
call_user_func('sound_' . $animal);
You can do it like this: 你可以这样做:
$animal = 'cow';
$sounder = "sound_$animal";
print ${sounder}();
However, a much better way would be to use an array: 但是,更好的方法是使用数组:
$sounds = array('dog' => sound_dog, 'cow' => sound_cow);
$animal = 'cow';
print $sounds[$animal]();
One of the advantages of the array method is that when you come back to your code six months later and wonder "gee, where is this sound_cow
function used?" 数组方法的一个优点是,当你六个月后回到你的代码并想知道“gee,这个
sound_cow
函数在哪里使用?” you can answer that question with a simple text search instead of having to follow all the logic that creates variable function names on the fly. 您可以使用简单的文本搜索来回答该问题,而不必遵循动态创建变量函数名称的所有逻辑。
http://php.net/manual/en/functions.variable-functions.php http://php.net/manual/en/functions.variable-functions.php
To do your example, you'd do 举个例子,你做的
$animal_function = "sound_$animal";
$animal_function();
You should ask yourself why you need to be doing this, perhaps you need to refactor your code to something like the following: 您应该问自己为什么需要这样做,也许您需要将代码重构为以下内容:
function animal_sound($type){
$animals=array();
$animals['dog'] = "woof";
$animals['cow'] = "moo";
return $animals[$type];
}
$animal = "cow";
print animal_sound($animal);
You can use $this->
and self::
for class-functions. 您可以使用
$this->
和self::
for class-functions。 Example provided below with a function input-parameter. 下面提供了一个带有函数输入参数的示例。
$var = 'some_class_function';
call_user_func(array($this, $var), $inputValue);
// equivalent to: $this->some_class_function($inputValue);
You can use curly brackets to build your function name. 您可以使用花括号来构建函数名称。 Not sure of backwards compatibility, but at least PHP 7+ can do it.
不确定向后兼容性,但至少PHP 7+可以做到这一点。
Here is my code when using Carbon to add or subtract time based on user chosen type (of 'add' or 'sub'): 这是我使用Carbon根据用户选择的类型('add'或'sub')添加或减去时间的代码:
$type = $this->date->calculation_type; // 'add' or 'sub'
$result = $this->contactFields[$this->date->{'base_date_field'}]
->{$type.'Years'}( $this->date->{'calculation_years'} )
->{$type.'Months'}( $this->date->{'calculation_months'} )
->{$type.'Weeks'}( $this->date->{'calculation_weeks'} )
->{$type.'Days'}( $this->date->{'calculation_days'} );
The important part here is the {$type.'someString'}
sections. 这里重要的部分是
{$type.'someString'}
部分。 This will generate the function name before executing it. 这将在执行之前生成函数名称。 So in the first case if the user has chosen 'add',
{$type.'Years'}
becomes addYears
. 因此,在第一种情况下,如果用户选择了“添加”,则
{$type.'Years'}
将成为addYears
。
For PHP >= 7
you can use this way: 对于
PHP >= 7
您可以使用以下方式:
function sound_dog() { return 'woof'; }
function sound_cow() { return 'moo'; }
$animal = 'cow';
print ('sound_' . $animal)();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.