2

I am trying to create a PHP function that takes another PHP function as input. Here is an example:

function getMean(function){
    $allUsers =  getAllUsers();
    $sum = 0;

    foreach ($allUsers  as $currentUser ){

        $sum =+ (function($currentUser['CONSUMER_ID'], 5, 8))/(8-5);

    }

}
2
  • can u explain your question a little better? Commented Mar 25, 2011 at 15:48
  • 1
    probably a duplicate of stackoverflow.com/questions/2700433/… Commented Mar 25, 2011 at 15:50

7 Answers 7

4

Perhaps something like this should do it. PHP has a "type" called callback, which can be a closure (as of PHP 5.3), name of a function or array containing object and the method to call. These callbacks can be called with call_user_func()

function getMean($callback){
    $allUsers =  getAllUsers();
    $sum = 0;

    foreach ($allUsers  as $currentUser ){
        $sum =+ (call_user_func($callback, $currentUser['CONSUMER_ID'], 5, 8))/(8-5);
    }

    return $sum;
}
Sign up to request clarification or add additional context in comments.

1 Comment

since you cant type hint you might want to warp it in a if block with is_callable($callback).
2

You need PHP 5.3 to do that natively.

function getMean($function){
    $allUsers =  getAllUsers();
    $sum = 0;

    foreach ($allUsers  as $currentUser ){

        $sum += ($function($currentUser['CONSUMER_ID'], 5, 8))/(8-5);
    }
    return $sum;
}

getMean(function($consumer_id, $five, $eight) {
    return $consumer_id;
});

I you run PHP 5.3- (lower than 5.3), you need to use a callback (documentation is here) along with the call_user_func() or call_user_func_array() function.

Comments

0

you can do that as long as function($currentUser['CONSUMER_ID'], 5, 8) returns something

Comments

0

It looks like the PHP Eval function is what you are looking for. Pass the getMean function a string, and then change the line to have $sum =+ (eval($param) .....)

Security is a problem though because any function can be passed.

2 Comments

No. The callback type is undoubtedly better here.
Learn something new every day
0

Save the function as a variable.

$function = function($param1){ //code here }

and pass it to your other function as a parameter.

function getMean($function)
{
  //code here
  $sum += $function($param);
}

[edit]

PHP.net Manual - Variable Functions

3 Comments

I believe this requires php >= 5.3.0
@whoughton It does not specify in the documentation, but I think you are right.
I think yours is actually an anonymous function/closure, as opposed to a variable function. php.net/manual/en/functions.anonymous.php
0

are you searching for anonymous functions?

Felix

Comments

0

If you want to dynamically create a function in php < 5.3 then youre stuck with using create_function. Becuase of how you use this function its really insane to use it for anything but but creating simple function for array_map,array_walk or things that do simple calculations or basic text processing.

If youre doing anything more complex than that its much easier and less of a headache to simply define the function as normal and then use it with call_user_func or call_user_func_array as others have suggested.

DO NOT USE eval :-)

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.