Home >Backend Development >PHP Tutorial >php call_user_func function
1. Function description
Call the callback function. Prototype: mixed call_user_func ( callable
Parameters cannot be references, but references can be passed through arrays. Here is an example:
<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></code>
Output:
0
1
Return value: Return the return value of the callback function, or FALSE if there is an error.
2. Example
<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></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></code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above introduces the php call_user_func function, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.