Accept function as parameter in PHP

后端 未结 8 783
Happy的楠姐
Happy的楠姐 2020-11-28 06:22

I\'ve been wondering whether is possible or not to pass a function as parameter in PHP; I want something like when you\'re programming in JS:

object.exampleM         


        
8条回答
  •  旧时难觅i
    2020-11-28 07:02

    According to @zombat's answer, it's better to validate the Anonymous Functions first:

    function exampleMethod($anonFunc) {
        //execute anonymous function
        if (is_callable($anonFunc)) {
            $anonFunc();
        }
    }
    

    Or validate argument type since PHP 5.4.0:

    function exampleMethod(callable $anonFunc) {}
    

提交回复
热议问题