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

自定义函数名解决思路

WBOY
Release: 2016-06-13 12:03:39
Original
1097 people have browsed it

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

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

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


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 />
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template