在PHP中使用变量作为函数名称 [英] Use Variable as Function Name in PHP

查看:115
本文介绍了在PHP中使用变量作为函数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

使用变量定义PHP函数 b

是否有一种使用变量作为函数名称的方法。



例如,我有一个变量

  $ varibaleA; 

我想创建一个函数,即

  function $ variableA(){
}

所以这个函数可以稍后在脚本中调用。有谁知道这是否可以做到?



谢谢 如果没有用 eval() include()

我认为根据您要做的事情,您需要存储匿名函数代替该变量(使用 create_function() 如果你不在PHP 5.3 + p>

  $ variableA = function(){
//做东西
};

您仍然可以像调用变量函数,如下所示:

  $ variableA的(); 


Possible Duplicate:
Use a variable to define a PHP function

Is 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() or include().

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆