Considerations for using call_user_func function in php

  • 2021-08-05 09:28:59
  • OfStack

This article illustrates the use of call_user_func function in php. Share it for your reference. The specific analysis is as follows:

Precautions for call_user_func function: parse error: syntax error, unexpected t_list, expecting t_string in. Today, when using this function, I directly prompt the above problems, and refer to the official manual and do not introduce the precautions for using it.

Attachment: mixed call_user_func (callback $function [, mixed $parameter [, mixed $...]). Any built-in or user-defined functions can be passed except for language constructs such as array (), echo (), empty (), eval (), exit (), isset (), list (), print () and unset ().

My problem is that there is a method name called list in the object, so it conflicts with the language structure list () of php tutorial.

Look at the example application: The call_user_func function is similar to a special method of calling functions, and the use method is as follows:

function a($b,$c)      
{   
echo  $b;   
echo  $c;   
}   
call_user_func('a',  "111","222");   
call_user_func('a',  "333","444");   
// Display   111  222  333  444

It is strange to call the method inside the class. I actually use array. I don't know how developers consider it. Of course, new is omitted, which is also full of new ideas. The code is as follows:
class  a  {    
function  b($c)     
{   
echo  $c;   
}   
}   
call_user_func(array("a",  "b"),"111");   
// Display   111

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 structure of the parameters clearer. The code is as follows:
function  a($b,  $c)      
{   
echo  $b;   
echo  $c;
}   
call_user_func_array('a',  array("111",  "222"));   
// Display   111  222

I hope this article is helpful to everyone's php programming.


Related articles: