我如何动态创建一个函数? [英] How can I create a function dynamically?

查看:89
本文介绍了我如何动态创建一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像 $ string =blah;

这样的变量如何创建一个函数有名称的变量值?
这是可能的PHP?

How can I create a function that has the variable value as name? Is this possible in PHP?

函数$ string($ args){...} 或什么的,并且可以像这样调用它:

Like function $string($args){ ... } or something, and be able to call it like:

blah($args);


推荐答案

这听起来不像是一个伟大的设计选择,它可能是值得重新思考的,但是...

That doesn't sound like a great design choice, it might be worth rethinking it, but...

如果您使用PHP 5.3,则可以使用匿名函数。

If you're using PHP 5.3 you could use an anonymous function.

<?php
$functionName = "doStuff";
$$functionName = function($args) {
    // Do stuff
};

$args = array();
$doStuff($args);
?>

这篇关于我如何动态创建一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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