1. 函数说明
调用回调函数。原型: mixed call_user_func ( callable
参数不可以为引用,但可以通过数组传递引用。下面是一个例子:
<code><span><?php </span><span><span>function</span><span>addone</span><span>(&<span>$num</span>)</span> {</span><span>$num</span> ++; } <span>$num</span> = <span>0</span>; call_user_func(<span>'addone'</span>, <span>$num</span>); <span>echo</span><span>"num : "</span> . <span>$num</span> . <span>"\n"</span>; call_user_func_array(<span>'addone'</span>, <span>array</span>(&<span>$num</span>) ); <span>echo</span><span>"num : "</span> . <span>$num</span> . <span>"\n"</span>; <span>?></span></span></code>
输出:
0
1
返回值:返回回调函数的返回值,如果错误则返回FALSE。
2. 示例
<code><span><?php </span><span>namespace</span><span>Foobar</span>; <span><span>class</span><span>Foo</span> {</span><span>static</span><span>public</span><span><span>function</span><span>test</span><span>()</span> {</span><span>print</span><span>"Hello world!\n"</span>; } } call_user_func(<span>__NAMESPACE__</span> .<span>'\Foo::test'</span>); call_user_func(<span>array</span>(<span>__NAMESPACE__</span> .<span>'\Foo'</span>, <span>'test'</span>)); <span>?></span></span></code>
<code><span><?php </span><span><span>class</span><span>myclass</span> {</span><span>static</span><span><span>function</span><span>test</span><span>()</span> {</span><span>echo</span><span>"Hello world!\n"</span>; } } <span>$classname</span> = <span>"myclass"</span>; call_user_func(<span>array</span>(<span>$classname</span>, <span>'test'</span>)); <span>$myobject</span> = <span>new</span> myclass(); call_user_func(<span>array</span>(<span>$myobject</span>, <span>'test'</span>)); <span>?></span></span></code>
以上就介绍了php call_user_func函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。