A brief analysis of the call user func () function in PHP and how to invoke a custom function _php instance using the called User func

Source: Internet
Author: User
Tags mixed

Ucenter source code has a function Call_user_func, beginning to think it is a function of their own definition, the results are nowhere to be found. It was later learned that Call_user_func is a built-in function of PHP that allows the user to invoke a directly written function and pass in a certain parameter, summarizing the use of the function below.

The Call_user_func function is similar to a particular method of calling a function, using the following method:

<?php
function Nowamagic ($a, $b) 
{ 
 echo $a; 
 echo $b; 
} 
Call_user_func (' Nowamagic ', "", ""); 
Call_user_func (' Nowamagic ', "", ""); 
Show  
?>

Call the internal method of the class is strange, incredibly with array, do not know how to consider the developer, of course, save the new, also quite innovative:

<?php
Class A { 
 function B ($c) 
 { 
  echo $c; 
 } 
} 
Call_user_func (Array ("A", "B"), ""); 
Show 

The Call_user_func_array function is very similar to the call_user_func, except that it passes the argument in a different way, making the parameter structure clearer:

<?php
function A ($b, $c) 
{ 
 echo $b; 
 echo $c; 
} 
Call_user_func_array (' A ', Array ("", "")); 
Show 

The Call_user_func_array function can also invoke methods within the class:

<?php
Class classa 
{ 
function BC ($b, $c) { 
  $BC = $b + $c; 
echo $bc; 
} 
} 
Call_user_func_array (Array (' ClassA ', ' BC '), Array ("", "")); 
Show 

Both the Call_user_func function and the Call_user_func_array function support references, which tend to be more functional consistent with normal function calls:

<?php
function A ($b) 
{ 
 $b + +; 
} 
$c =; 
Call_user_func (' A ', $c); 
echo $c//Show 
Call_user_func_array (' A ', Array ($c)); 
echo $c//Display 
?>
In addition, both call_user_func functions and Call_user_func_array functions support references.
View Sourceprint?
<?php
function Increment (& $var)
{
 $var + +;
}
$a =;
Call_user_func (' increment ', $a);
echo $a; 
call_user_func_array (' Increment ', array (& $a));//can use this instead
echo $a;// 
?>

Here's an introduction to using Call_user_func to invoke custom functions

Using the Call_user_func function, you can call custom functions by passing in string functions and support references.

1.mixed Call_user_func (Callable $callback [, Mixed $parameter [, mixed $ ...]])

Invokes the custom function provided by the first argument, followed by the parameters of the custom function, and returns the result of the custom function

function say ($word)
{
 echo $word;
}
Call_user_func (' say ', ' Hello World '); Hello World
Of course you can also call methods in a class:
class A {
 function say ($word = ')
 {
  echo $word;
 }
} 
$a = new A (); Note that it must be instantiated unless it is a static
Call_user_func (Array ($a, ' say '), ' Hello World ');//hello world

2.mixed Call_user_func_array (callable $callback, array $param _arr)

In other words call_user_func_array and call_user_func function functions, just call the parameters a little different:

function A ($word)
{
 echo $word;
}
Class A {
 function say ($word = ')
 {
  echo $word;
 }
}
Call_user_func_array (' A ', array (' Hello World ')); Hello world
$a = new A ();/Note that must be instantiated unless it is a static
Call_user_func_array (Array ($a, ' say '), array (' Hello World)); Hello World

Note: The other similar two functions are Call_user_method and Call_user_method_array (), but they are deactivated after PHP4.1.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.