Home  >  Article  >  Backend Development  >  Analyze how to call user-defined functions in php_PHP tutorial

Analyze how to call user-defined functions in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:59:18834browse

Let’s start with other people’s examples:
The call_user_func function is similar to a special method of calling a function. The usage is as follows:

Copy Code The code 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’s strange to call the method inside the class. It actually uses array. I don’t know what the developer thought about it. Of course, new is omitted, which is also full of novelty:  
Copy code 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:
Copy code The code is as follows :

function a($b, $c)
{
echo $b;
echo $c;

}
call_user_func_array(' a', array("111", "222"));
//Display 111 222
?>

The call_user_func_array function can also call methods inside the class
Copy code The code is as follows:

Class ClassA 


function bc($b, $c ) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array('ClassA','bc'), array("111" , "222"));

//Display 333 Tend to have consistent functionality:


Copy code
The code is as follows: function a($b) {
$b++;
}
$c = 0;
call_user_func('a', $c);
echo $c;//Display 1
call_user_func_array('a', array( $c));
echo $c;//Display 2


Another: both the call_user_func function and the call_user_func_array function support references.


Copy code
The code is as follows:function increment(&$var)
{
$var++;
}
$a = 0;
call_user_func('increment', $a);
echo $a; // 0
call_user_func_array('increment', array(&$a)); // You can use this instead
echo $a; // 1
?>




http://www.bkjia.com/PHPjc/328151.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/328151.htmlTechArticleLet’s start with other people’s examples: The call_user_func function is similar to a special method of calling a function. The method of use is as follows : Copy the code as follows: function a($b,$c) { echo $b;...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn