doujiang7258 2011-12-11 18:45
浏览 200
已采纳

在PHP中使用变量作为函数名[重复]

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

  • 写回答

3条回答 默认 最新

  • 普通网友 2011-12-11 18:48
    关注

    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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 安装 opencv4nodejs 报错
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!