1

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

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

and I have 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...

2 Answers 2

2

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");
Sign up to request clarification or add additional context in comments.

1 Comment

Well, closures have been added in PHP 5.3. Only public is invalid here.
2

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);
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.