php php中的动态函数名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15004890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic function name in php
提问by Baylock
I would like to simplify the creation of "Custom Post Types" in WordPress as it is tedious to go through the same script and change all the custom post type name instances manually over and over.
我想简化在 WordPress 中“自定义帖子类型”的创建,因为通过相同的脚本并一遍又一遍地手动更改所有自定义帖子类型名称实例很乏味。
It's quite simple to achieve by creating a variable containing the CPT name and use it everywhere it is needed. This way, All I have to do is declare the variable in the beginning of the script and that should take care of the rest.
通过创建一个包含 CPT 名称的变量并在需要的任何地方使用它来实现这一点非常简单。这样,我所要做的就是在脚本的开头声明变量,剩下的就应该处理好了。
The only issue is that, to make it work, I also need to prefix the CPT name in front of every function inside the script and it seems that using a variable in a function name is not easy or even recommended in PHP.
唯一的问题是,为了使它工作,我还需要在脚本中的每个函数前面加上 CPT 名称的前缀,而且在函数名称中使用变量似乎并不容易,甚至在 PHP 中也不推荐。
So how could I solve this?
那我怎么能解决这个问题呢?
Here is an example below to make it clear:
下面是一个例子来说明:
$prefix = 'news';
function news_custom_type_init()
{
global $prefix;
register_post_type($prefix, array(
'labels' => array(
'name' => $prefix,
'singular_label' => $prefix,
'add_new' => 'Add',
...
));
register_taxonomy_for_object_type( 'category', $prefix );
}
add_action('init', $prefix.'_custom_type_init');
This is almost fine and could be standardized if only I could dynamically rename the function in order not to have to write the word "news" in front of it but use the "$prefix" instead.
这几乎没问题,并且可以标准化,只要我可以动态重命名函数,以便不必在它前面写“新闻”这个词,而是使用“$prefix”。
This could have been nice but just doesn't work:
这本来很好,但不起作用:
$prefix = 'news';
$functionName= $prefix."_custom_type_init";
function $functionName()
{
global $prefix;
register_post_type($prefix, array(
'labels' => array(
'name' => $prefix,
'singular_label' => $prefix,
'add_new' => 'Add',
...
));
register_taxonomy_for_object_type( 'category', $prefix );
}
add_action('init', $prefix.'_custom_type_init');
Having to name manually the function kinda defeat the original purpose of my attempt (especially when the script embeds dozens of functions like this one).
必须手动命名函数有点违背了我尝试的最初目的(尤其是当脚本嵌入了几十个这样的函数时)。
What would be the best way to do this?
什么是最好的方法来做到这一点?
PS: I googled and "stackoverflowed" a lot about this but didn't find any working solution that fit my needs and doesn't generate a WordPress error message.
PS:我在谷歌上搜索了很多关于这个的“stackoverflowed”,但没有找到任何适合我需要的工作解决方案,也没有生成 WordPress 错误消息。
Thank you.
谢谢你。
回答by Nick Andriopoulos
Simple enough, here's a similar snipped form a project:
很简单,这是一个类似的项目截图:
$function = $prefix . '_custom_type_init';
if(function_exists($function)) {
$function();
}
回答by David Vielhuber
This is an old thread but simply use anonymous functions:
这是一个旧线程,但只需使用匿名函数:
add_action('init', function() use($args) {
//...
});
Then there is no need to declare so many functions.
那么就没有必要声明这么多函数了。
回答by ryczypior
I think you could use
我想你可以用
runkit_function_add
runkit_function_add
http://php.net/manual/pl/function.runkit-function-add.php
http://php.net/manual/pl/function.runkit-function-add.php
One other available method is to use eval()
另一种可用的方法是使用 eval()
回答by cernunnos
Edit (2017-04): Anonymous functions (properly implemented) are the way to go, see answer by David Vielhuber.
编辑(2017-04):匿名函数(正确实现)是要走的路,请参阅 David Vielhuber 的回答。
This answer is ill advised, as is any approach that involves code as a string, because this invites (among other things) concatenation.
这个答案是不明智的,任何涉及代码作为字符串的方法也是如此,因为这会邀请(除其他外)串联。
Im not sure if it is advisable to use, or if it helps you, but php allows you to create "anonymous" functions :
我不确定使用它是否可取,或者它是否对您有帮助,但 php 允许您创建“匿名”函数:
function generateFunction ($prefix) {
$funcname = create_function(
'/* comma separated args here */',
'/* insert code as string here, using $prefix as you please */'
);
return $funcname;
}
$func= generateFunction ("news_custom_type_init");
$func(); // runs generated function
I am assuming add_action just calls whatever function you passed.
我假设 add_action 只是调用您传递的任何函数。
http://php.net/manual/en/function.create-function.php
http://php.net/manual/en/function.create-function.php
Note: create_function will not return a function with the name you want, but the contents of the function will be under your control, and the real name of the function is not important.
注意:create_function 不会返回你想要的名字的函数,但是函数的内容会在你的控制之下,函数的真实名字并不重要。
回答by Terry Lin
This post is old, but PHP 7 may solve this question.
这篇文章很旧,但 PHP 7 可能会解决这个问题。
Try:
尝试:
$prefix = 'news';
$functionName = $prefix . "_custom_type_init";
${$functionName} = function() use ($prefix) {
register_post_type($prefix, array(
'labels' => array(
'name' => $prefix,
'singular_label' => $prefix,
'add_new' => 'Add'
)
);
register_taxonomy_for_object_type( 'category', $prefix );
};
add_action('init', '$' . $functionName);
I think it should work for WordPress.
我认为它应该适用于 WordPress。

