Home > Article > Backend Development > PHP calls function through string_PHP tutorial
1. call_user_func
<span>function</span> a<span>(</span><span>$b</span><span>,</span><span>$c</span><span>)</span><span>{</span> <span>echo</span> <span>$b</span><span>;</span> <span>echo</span> <span>$c</span><span>;</span> <span>}</span> <span>call_user_func</span><span>(</span><span>'a'</span><span>,</span> <span>"111"</span><span>,</span><span>"222"</span><span>)</span><span>;</span> <span>call_user_func</span><span>(</span><span>'a'</span><span>,</span> <span>"333"</span><span>,</span><span>"444"</span><span>)</span><span>;</span> <span>//显示 111 222 333 444</span> <span>?></span> <span>//调用类内部的方法比较奇怪,居然用的是array,不知道开发者是如何考虑的,当然省去了new,也是满有新意的:</span> <span>class</span> a <span>{</span> <span>function</span> b<span>(</span><span>$c</span><span>)</span><span>{</span> <span>echo</span> <span>$c</span><span>;</span> <span>}</span> <span>}</span> <span>call_user_func</span><span>(</span><span>array</span><span>(</span><span>"a"</span><span>,</span> <span>"b"</span><span>)</span><span>,</span><span>"111"</span><span>)</span><span>;</span> <span>//显示 111</span> <span>?></span>
2. call_user_func_array
The call_user_func_array function is very similar to call_user_func, except that the parameters are passed in a different way to make the parameter structure clearer:
<span>function</span> a<span>(</span><span>$b</span><span>,</span> <span>$c</span><span>)</span><span>{</span> <span>echo</span> <span>$b</span><span>;</span> <span>echo</span> <span>$c</span><span>;</span> <span>}</span> <span>call_user_func_array</span><span>(</span><span>'a'</span><span>,</span> <span>array</span><span>(</span><span>"111"</span><span>,</span> <span>"222"</span><span>)</span><span>)</span><span>;</span> <span>//显示 111 222</span> <span>?></span> <span>//call_user_func_array函数也可以调用类内部的方法的</span> <span>Class</span> ClassA<span>{</span> <span>function</span> bc<span>(</span><span>$b</span><span>,</span> <span>$c</span><span>)</span> <span>{</span> <span>$bc</span> <span>=</span> <span>$b</span> <span>+</span> <span>$c</span><span>;</span> <span>echo</span> <span>$bc</span><span>;</span> <span>}</span> <span>}</span> <span>call_user_func_array</span><span>(</span><span>array</span><span>(</span><span>'ClassA'</span><span>,</span><span>'bc'</span><span>)</span><span>,</span> <span>array</span><span>(</span><span>"111"</span><span>,</span> <span>"222"</span><span>)</span><span>)</span><span>;</span> <span>//显示 333</span> <span>?></span>
Both the call_user_func function and the call_user_func_array function support references, which makes them more functionally consistent with ordinary function calls:
<span>function</span> a<span>(</span><span>&</span><span>$b</span><span>)</span><span>{</span> <span>$b</span><span>++;</span> <span>}</span> <span>$c</span> <span>=</span> <span>0</span><span>;</span> <span>call_user_func</span><span>(</span><span>'a'</span><span>,</span> <span>&</span><span>$c</span><span>)</span><span>;</span> <span>echo</span> <span>$c</span><span>;</span><span>//显示 1</span> <span>call_user_func_array</span><span>(</span><span>'a'</span><span>,</span> <span>array</span><span>(</span><span>&</span><span>$c</span><span>)</span><span>)</span><span>;</span> <span>echo</span> <span>$c</span><span>;</span><span>//显示 2</span>