Pass a function by reference in PHP

前端 未结 9 1737
灰色年华
灰色年华 2020-12-29 03:17

Is it possible to pass functions by reference?

Something like this:

function call($func){
    $func();
}

function test(){
    echo \"hello world!\";         


        
9条回答
  •  清歌不尽
    2020-12-29 03:58

    In PHP 5.4.4 (haven't tested lower or other versions), you can do exactly as you suggested.

    Take this as an example:

    function test ($func) {
        $func('moo');
    }
    
    function aFunctionToPass ($str) {
        echo $str;
    }
    
    test('aFunctionToPass');
    

    The script will echo "moo" as if you called "aFunctionToPass" directly.

提交回复
热议问题