多个自定义函数该如何传

WBOY
Release: 2016-06-13 12:54:29
Original
925 people have browsed it

多个自定义函数该怎么传?

<br />
function a(){}<br />
function b(){}<br />
function c(){}<br />
<br />
function demo(参数){<br />
 //有时用a(),有时用b(),有时用c()<br />
}<br />
<br />
demo(参数);<br />
Copy after login


我的疑问是,可以把函数作为参数传递么?
可以的话,写法是怎样的?
求指点。。。


------解决方案--------------------
本帖最后由 xuzuning 于 2013-01-28 16:06:55 编辑

可以把函数作为参数传递么?
可以!
//无其他参数<br />
function demo($func) {<br />
  $func();<br />
}<br />
//例<br />
demo('a');<br />
<br />
//有固定数量参数,比如2个<br />
function demo($func, $p1, $p2) {<br />
  $func($p1, $p2);<br />
}<br />
//例<br />
demo('b', 2, 4);<br />
<br />
//有不定数量参数<br />
function demo() {<br />
  $t = func_get_ags();<br />
  $func = array_shift($t);<br />
  call_user_func_array($func, $t);<br />
}<br />
//例<br />
demo('c', 2, 4, 'a', 'b', 'c');<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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!