Home >Backend Development >PHP Tutorial >自定义函数名解决思路

自定义函数名解决思路

WBOY
WBOYOriginal
2016-06-13 12:03:391098browse

自定义函数名
请问一下,PHP是否支持自定义函数在声明时,函数名称采用变量形式,如何使用?
我试过用
function $a{}
$a = 'test';
test();
?>
提示错误。
------解决方案--------------------
你可以通过类来实现(__call)

------解决方案--------------------
1.使用eval

<br />$a = 'test';<br />eval("function $a(){ echo 'function name is:'.__FUNCTION__;}");<br />test();<br />


2.使用class 的魔术方法__call
<br />class foo{<br />    public function __call($name, $param){<br />        if($name=='test'){<br />            echo 'test';<br />        }else{<br />            echo 'name not exists';<br />        }<br />    }<br />}<br /><br />$obj = new foo();<br />$a = 'test';<br />$obj->$a();<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn