即时(动态)创建函数名称 - PHP
Creating a function name on the fly (dynamically) - PHP
下面是我正在使用的代码。
function my_tab( $tabs ) {
// the following array will be created dynamically
$colors = array("red", "white", "blue");
foreach ($colors as $value) {
$tabs[$value] = array(
'name' => $value
);
// start creating functions
function content_for_[$value]() {
echo "this is the " .$value. " tab";
}
// stop creating functions
add_action('content_for_'.$value, 'content_for_'.$value);
}
return $tabs;
}
如您所见,我有一个动态创建的数组。我需要为每个 color
创建一个 function
。这些函数绑定到 hooks
,函数名称必须绑定到 exist
。到目前为止,我在过去 6 小时内尝试的所有操作都会导致类似于以下内容的错误:
"call_user_func_array() expects parameter 1 to be a valid callback,
function 'content_for_green' not found or invalid function name"
如果您使用 PHP >= 5.3,您可以使用 anonymous functions,例如
add_action( 'content_for_' . $value, function() use ( $value ) {
echo "this is the " . $value . " tab";
}
);
使用 use
关键字允许匿名函数从当前范围捕获变量(例如,在您的情况下为 $value
)。
你肯定不想那样做,这是很糟糕的做法。使用闭包,,明显不那么丑陋,但仍然很难保持巫毒魔法。
相反,只需向您的回调添加一个参数,例如通过扩展现有参数数组:
function content_for_color_callback($args) {
echo 'this is the ' . $args['color'] . ' tab';
}
只需注册钩子:
add_action('content_for_color', 'content_for_color_callback');
... 并将 color
作为标准参数传递,:
// do not do this
do_action("content_for_{$nav}", $args);
// do this instead
$args['color'] = $nav;
do_action('content_for_color', $args);
这种方法的众多优点之一是您可以减少隐蔽错误的空间。例如,假设有人想要调用一个不存在的 content_for_bazinga()
函数。用你的方法,你会得到一个模糊的
Call to undefined function content_for_bazinga()
比较:
function content_for_color_callback($args) {
$color = $args['color'];
if( !colorIsValid($color)) {
throw new Exception("There is no such tab: $color");
}
echo "This is the $color tab";
}
下面是我正在使用的代码。
function my_tab( $tabs ) {
// the following array will be created dynamically
$colors = array("red", "white", "blue");
foreach ($colors as $value) {
$tabs[$value] = array(
'name' => $value
);
// start creating functions
function content_for_[$value]() {
echo "this is the " .$value. " tab";
}
// stop creating functions
add_action('content_for_'.$value, 'content_for_'.$value);
}
return $tabs;
}
如您所见,我有一个动态创建的数组。我需要为每个 color
创建一个 function
。这些函数绑定到 hooks
,函数名称必须绑定到 exist
。到目前为止,我在过去 6 小时内尝试的所有操作都会导致类似于以下内容的错误:
"call_user_func_array() expects parameter 1 to be a valid callback, function 'content_for_green' not found or invalid function name"
如果您使用 PHP >= 5.3,您可以使用 anonymous functions,例如
add_action( 'content_for_' . $value, function() use ( $value ) {
echo "this is the " . $value . " tab";
}
);
使用 use
关键字允许匿名函数从当前范围捕获变量(例如,在您的情况下为 $value
)。
你肯定不想那样做,这是很糟糕的做法。使用闭包,
相反,只需向您的回调添加一个参数,例如通过扩展现有参数数组:
function content_for_color_callback($args) {
echo 'this is the ' . $args['color'] . ' tab';
}
只需注册钩子:
add_action('content_for_color', 'content_for_color_callback');
... 并将 color
作为标准参数传递,:
// do not do this
do_action("content_for_{$nav}", $args);
// do this instead
$args['color'] = $nav;
do_action('content_for_color', $args);
这种方法的众多优点之一是您可以减少隐蔽错误的空间。例如,假设有人想要调用一个不存在的 content_for_bazinga()
函数。用你的方法,你会得到一个模糊的
Call to undefined function content_for_bazinga()
比较:
function content_for_color_callback($args) {
$color = $args['color'];
if( !colorIsValid($color)) {
throw new Exception("There is no such tab: $color");
}
echo "This is the $color tab";
}