1

I have a function variable like this...

$aName = "My Name";

The function need to pass in like this:

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

Than, I have a method that responsible for execute the sayHelloFunction:

runningFunction($sayHelloFunction($aName));

in the runningFunction, it will have some condition to execute the function, but when I pass the "$sayHelloFunction($aName)" to runningFunction, it execute automatically, but I would like to pass the variable $aName as well, how can I achieve it? Thank you.

2
  • i think you are confusing php and javascript Commented May 1, 2011 at 8:29
  • 1
    @Ibrahim - this is a perfectly valid PHP question. Commented May 1, 2011 at 8:31

5 Answers 5

2
runningFunction($sayHelloFunction, $aName);

Simples.

Sign up to request clarification or add additional context in comments.

1 Comment

but my sayHelloFunction requires param, and I don't want to assign the param inside the runningFunction.....
1

You will have to pass the arguments separately. However, you could wrap them in an array so that you can pass them to runningFunction as a single argument, like this:

$printFunction = function($args) {
    print $args['lastname'].', '.$args['firstname'];
};

function runningFunction($f, $a) {
    $f($a);
}

$firstname = 'Bob';
$lastname = 'Smith';

$functionArguments = array(
    'firstname' => $firstname,
    'lastname' => $lastname
);

runningFunction($printFunction, $functionArguments);

If you want your dynamic functions to get "proper" arguments, then I see no way around something like this:

function runningFunction($f, $a) {
    switch(count($a)) {
        0: $f(); break;
        1: $f($a[0]); break;
        2: $f($a[0], $a[1]); break;
        3: $f($a[0], $a[1], $a[2]); break;
        // and so on
    }
}

1 Comment

call_user_func_array() basically does what you did in your switch statement.
1

Pass the parameters as an array, and then use call_user_func_array() to call your function.

This way your runningFunction() will be absolutely abstract (as you requested), it can call any type of function, it's your responsibility to pass the right number of parameters.

function runningFunction ($callback, $parameters=array()) {
    call_user_func_array($callback, $parameters);
}

runningFunction($sayHelloFunction, array($aName));

call_user_func_array()

Comments

0

as xconspirisist suggested pass $aName as a seperate parameter to the function.

Details on Variable Functions can be found on the PHP site.

1 Comment

I want to make the runningFunction abstract, that don't depends on the $sayHelloFunction, I don't mind pass runningFunction one more param, but I am considering, if I have one more param require, like $sayHelloFunctionAgain, which requires two params, $aName, $aTime, then, I need to write one more params for runningFunction, that is not wt I want....
0

Use an anonymous function when calling runningFunction

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

runningFunction(function() {
    $sayHelloFunction($aName));
    // You can do more function calls here...
});

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.