开发者

How to set variable in a function variable in php?

I have a function variable like this... $aName = "My Name";

$sayHelloFunction = public function sayHello($aName){
       echo($aName);
    }  

and I hav开发者_JAVA技巧e something like this.....

callAFunctionFromFunction($sayHelloFunction);

Inside the "callAFunctionFromFunction", I do this:

            if(is_callable($sayHelloFunction)) { 
                  $sayHelloFunction(); 
        }   

I found that the "My Name" can't display, what did I do wrong...


I suggest you to look here and here as these exact threads deal with passing a function as a parameter. Also closures (anonymous functions) in PHP have no name (that's why they are called anonymous), so what you should do is something like that.

<?php $sayHelloFunction = function($aName){
   echo($aName);
};
if(is_callable($sayHelloFunction)) $sayHelloFunction("Testing 1,2,3");


The function sayHello expects a parameter $aName, but when you call it you don't pass in a value.

You would need to do this:

    if(is_callable($sayHelloFunction)) { 
              $sayHelloFunction("Hello John"); 
    }   

Also you can't use the public access type with closures.

 $sayHelloFunction = function sayHello($aName) {
    echo($aName);
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜