PHP中is_callable()函数的用法详解

关于PHP中的is_callable()函数,手册中是这样说的“检测参数是否为合法的可调用结构”,但是没有很好的演示代码,所以今天我在我的WEB开发笔记上发一下演示代码,希望能给大家对is_callable()的理解上有一些帮助。演示代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<span style="color: #000000;font-weight: bold"><?php</span>
<span style="color: #000000;font-weight: bold">class</span> test <span style="color: #009900">{</span>
	static <span style="color: #000000;font-weight: bold">function</span> a<span style="color: #009900">(</span><span style="color: #009900">)</span> <span style="color: #009900">{</span>
		<span style="color: #b1b100">return</span> <span style="color: #0000ff">"test"</span><span style="color: #339933">;</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
<span style="color: #000000;font-weight: bold">class</span> abc <span style="color: #009900">{</span>
	<span style="color: #000000;font-weight: bold">public</span> <span style="color: #000000;font-weight: bold">function</span> a<span style="color: #009900">(</span><span style="color: #000088">$Object</span><span style="color: #339933">,</span><span style="color: #000088">$funcName</span><span style="color: #009900">)</span><span style="color: #009900">{</span>	
		<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #339933">!</span><a rel="noopener noreferrer nofollow" href="http://www.php.net/is_callable" rel="noopener noreferrer nofollow" target="_blank"><span style="color: #990000">is_callable</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><span style="color: #000088">$Object</span><span style="color: #339933">,</span><span style="color: #000088">$funcName</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//此处array($object,$funcname)的用法,相当于$object::$funcname调用$object的$funcname方法</span>
			<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"Error"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span><span style="color: #b1b100">else</span> <span style="color: #009900">{</span>
			<span style="color: #b1b100">echo</span><span style="color: #009900">(</span><span style="color: #0000ff">"OK"</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
		<span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>
 
<span style="color: #000088">$abc</span><span style="color: #339933">=</span><span style="color: #000000;font-weight: bold">new</span> abc<span style="color: #009900">(</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
<span style="color: #000088">$abc</span><span style="color: #339933">-></span><span style="color: #004000">a</span><span style="color: #009900">(</span><span style="color: #0000ff">"test"</span><span style="color: #339933">,</span><span style="color: #0000ff">"aa"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//test类下没有aa方法,会输出error</span>
<span style="color: #000088">$abc</span><span style="color: #339933">-></span><span style="color: #004000">a</span><span style="color: #009900">(</span><span style="color: #0000ff">"test"</span><span style="color: #339933">,</span><span style="color: #0000ff">"a"</span><span style="color: #009900">)</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//test类下有a方法,会输出ok</span>
<span style="color: #000000;font-weight: bold">?></span>

OK ,这个函数的用法,如果有什么问题,可以加我的PHP进阶群(63417682)做进一步的了解。