在PHP中使用变量作为函数名称 [英] Use Variable as Function Name in PHP
问题描述
可能存在重复:
使用变量定义PHP函数 b是否有一种使用变量作为函数名称的方法。
例如,我有一个变量
$ varibaleA;
我想创建一个函数,即
function $ variableA(){
}
所以这个函数可以稍后在脚本中调用。有谁知道这是否可以做到?
谢谢 如果没有用
eval()
或include()$ c弄脏你的手,那么声明一个带有变量但任意名称的函数是不可能的$ c $。
我认为根据您要做的事情,您需要存储匿名函数代替该变量(使用
create_function()
如果你不在PHP 5.3 + p>
$ variableA = function(){
//做东西
};
您仍然可以像调用变量函数,如下所示:
$ variableA的();
Possible Duplicate:
Use a variable to define a PHP functionIs there a way of using a variable as a function name.
For example i have a variable
$varibaleA;
and I want to create a function i.e.
function $variableA() { }
so this function can be called later in the script. Does anyone know if this can be done?
Thanks
解决方案Declaring a function with a variable but arbitrary name like this is not possible without getting your hands dirty with
eval()
orinclude()
.I think based on what you're trying to do, you'll want to store an anonymous function in that variable instead (use
create_function()
if you're not on PHP 5.3+):$variableA = function() { // Do stuff };
You can still call it as you would any variable function, like so:
$variableA();
这篇关于在PHP中使用变量作为函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!