Home  >  Article  >  Backend Development  >  How to implement php callback function?

How to implement php callback function?

怪我咯
怪我咯Original
2017-06-19 13:09:451248browse

GlobalCallback function

The global function here means that it is defined directly using function Function, which is not contained in any object or class. Please see the example below.

Sample code:

function fnCallBack( $msg1 , $msg2 )
{
    echo 'msg1:'.$msg1;
    echo "<br />n";
    echo &#39;msg2:&#39;.$msg2;
}
$fnName = "fnCallBack";
$params = array( &#39;hello&#39; , &#39;world&#39; );
call_user_func_array( $fnName , $params );

Code description:
The PHP built-in function call_user_func_array is used here to make the call. call_user_func_array has two parameters. The first parameter is a string , which represents the name of the function to be called. The second parameter is an array, which represents the parameter list, which will be passed to the function to be called in order. .
The effect is as follows:

The callback of the static method of the class:

If The method we want to call back is a static method of a class, so what should we do? We can still use PHP's built-in call_user_func_array method to make calls. Please see the example:

class MyClass
{
    public static function fnCallBack( $msg1 , $msg2 )
    {
        echo &#39;msg1:&#39;.$msg1;
        echo "<br />n";
        echo &#39;msg2:&#39;.$msg2;
    }
}
 
$className = &#39;MyClass&#39;;
$fnName = "fnCallBack";
$params = array( &#39;hello&#39; , &#39;world&#39; );
call_user_func_array( array( $className , $fnName ) , $params );

Code description:

This code is very similar to the code of the first method. We change the class name (MyClass) is also passed in as the first parameter of call_user_func_array, and the callback of the static method of the class can be implemented. Note that the first parameter of call_user_func_array is an array at this time. The first element of the array is the class name, and the second element is the name of the function to be called.

Run result:


(In fact, the result is the same as the first method ^_^)
Continue to study
If I use this method to call a non-static method of a class (that is, remove the static) , what will be the result? Please see the following code

class MyClass
{
    public function fnCallBack( $msg1 , $msg2 )
    {
        echo &#39;msg1:&#39;.$msg1;
        echo "<br />n";
        echo &#39;msg2:&#39;.$msg2;
    }
}
 
$className = &#39;MyClass&#39;;
$fnName = "fnCallBack";
$params = array( &#39;hello&#39; , &#39;world&#39; );
call_user_func_array( array( $className , $fnName ) , $params );

The running result is still the same as the picture above!

Object method callback:

    我先用最原始的字符串形式的调用方法尝试了一下,如下所示:

class MyClass
{
    private $name = &#39;abc&#39;;
    public function fnCallBack( $msg1 = &#39;default msg1&#39; , $msg2 = &#39;default msg2&#39; )
    {
        echo &#39;object name:&#39;.$this->name;
        echo "<br />n";
        echo &#39;msg1:&#39;.$msg1;
        echo "<br />n";
        echo &#39;msg2:&#39;.$msg2;
    }
}
 
$myobj = new MyClass();
$fnName = "fnCallBack";
$params = array( &#39;hello&#39; , &#39;world&#39; );
$myobj->$fnName();

成功了,输出结果

调用是成功了,不过如何把参数params传给这个方法呢,如果把params直接传进去,那么它会作为1个参数,怎么把params拆开来传进去呢?
查了下PHP手册,找到了create_function函数,这个方法可以用字符串来创建一个匿名函数,好,有思路了,可以创建一个匿名的函数,在这个匿名函数中,调用我们的回调函数,并把参数传进去。

class MyClass
{
    private $name = &#39;abc&#39;;
    public function fnCallBack( $msg1 = &#39;default msg1&#39; , $msg2 = &#39;default msg2&#39; )
    {
        echo &#39;object name:&#39;.$this->name;
        echo "<br />n";
        echo &#39;msg1:&#39;.$msg1;
        echo "<br />n";
        echo &#39;msg2:&#39;.$msg2;
    }
}
 
$myobj = new MyClass();
$fnName = "fnCallBack";
$params = array( &#39;hello&#39; , &#39;world&#39; );
$strParams = &#39;&#39;;
 
$strCode = &#39;global $myobj;global $fnName;global $params;$myobj->$fnName(&#39;;
for ( $i = 0 ; $i < count( $params ) ; $i ++ )
{
    $strParams .= ( &#39;$params[&#39;.$i.&#39;]&#39; );
    if ( $i != count( $params )-1 )
    {
        $strParams .= &#39;,&#39;;
    }
}
$strCode = $strCode.$strParams.");";
$anonymous = create_function( &#39;&#39; , $strCode);
$anonymous();

The above is the detailed content of How to implement php callback function?. For more information, please follow other related articles on the PHP Chinese website!

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