开发者

php defined named constant as a function name

How to create a dynamic function name dependent on a defined named constant?

eg:

define('NAME1','func1');

function NAME1($arg) {
    echo "function 1 $arg\n";
    print __FUNCTION__." in ".__开发者_如何学运维FILE__." at ".__LINE__."\n";
};

NAME1("helllo");

this echos function name as "NAME1" - why is it not func1?


The closest thing that I can think of is to use PHP 5.3 anonymous functions (other than using eval)

define('NAME1', 'my_function');
$func_name = NAME1;
$$func_name = function($arg) {
   // Your code
};
$my_function('test');


You can't do that.

this echos function name as "NAME1" - why is it not func1?

because you defined your function name as NAME1.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜