PHP中call_user_func_array()函数的用法演示

今天,看了一段代码,里面用到了很多call_user_func_array()函数,一开始,也是非常的迷糊,后来经过查手册发现,call_user_func_array()函数还是很好用的,所以把PHP中call_user_func_array()函数用法用简单的代码示例来说明一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">function</span> test<span style="color: #009900">(</span><span style="color: #000088">$str</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
	<span style="color: #b1b100">echo</span> <span style="color: #000088">$str</span><span style="color: #339933">;</span>
<span style="color: #009900">}</span>
 
<a rel="noopener noreferrer nofollow" href="http://www.php.net/call_user_func_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">call_user_func_array</span></a><span style="color: #009900">(</span><span style="color: #0000ff">"test"</span><span style="color: #339933">,</span><span style="color: #0000ff">"NO.1 www.chhua.com"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//输出"NO.1 www.chhua.com"</span>
<span style="color: #666666;font-style: italic">//参数说明“第一个参数是函数名,第二个是参数</span>
<span style="color: #000000;font-weight: bold">class</span> testClass <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> write<span style="color: #009900">(</span><span style="color: #000088">$str</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
		<span style="color: #b1b100">echo</span> <span style="color: #000088">$str</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<a rel="noopener noreferrer nofollow" href="http://www.php.net/call_user_func_array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">call_user_func_array</span></a><span style="color: #009900">(</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/array" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">array</span></a><span style="color: #009900">(</span>testClass<span style="color: #339933">,</span>write<span style="color: #009900">)</span><span style="color: #339933">,</span><span style="color: #0000ff">"NO.1 www.chhua.com"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//用类调用的时侯,用array(),array(类名,方法名)</span>
<span style="color: #000000;font-weight: bold">?></span>