PHP call_user_func vs. just calling function

前端 未结 8 1564
我寻月下人不归
我寻月下人不归 2020-12-04 08:06

I\'m sure there\'s a very easy explanation for this. What is the difference between this:

function barber($type){
    echo \"You wanted a $type haircut, no         


        
8条回答
  •  离开以前
    2020-12-04 08:36

    Although you can call variable function names this way:

    function printIt($str) { print($str); }
    
    $funcname = 'printIt';
    $funcname('Hello world!');
    

    there are cases where you don't know how many arguments you're passing. Consider the following:

    function someFunc() {
      $args = func_get_args();
      // do something
    }
    
    call_user_func_array('someFunc',array('one','two','three'));
    

    It's also handy for calling static and object methods, respectively:

    call_user_func(array('someClass','someFunc'),$arg);
    call_user_func(array($myObj,'someFunc'),$arg);
    

提交回复
热议问题