0

I have created function

function do_stuff($text) {
   $new_text = nl2br($text);

   return $new_text;
}

$result = do_stuff("Hello \n World!"); 
//returns "Hello <br /> World!"

I want to be able to supply another simple built in PHP function e.g. strtoupper() inside my function somehow, its not just strtoupper() that i need, i need ability to supply different functions into my do_stuff() function.

Say i want to do something like this.

$result = do_stuff("Hello \n World!", "strtolower()");
//returns "Hello <br /> World!"

How would i make this work without creating another function.

function do_stuff($text, $sub_function='') {
   $new_text = nl2br($text);

   $sub_function($new_text);

   return $new_text;
}

$result = do_stuff("Hello \n World!"); 
//returns "Hello <br /> World!"

P.S. Just remembered variable variables, and googled, there's actually is Variable functions too, might answer this one myself.

http://php.net/manual/en/functions.variable-functions.php

Roman Toasov
  • 747
  • 11
  • 36
  • I don't understand. What you're asking you have achieved in your second example. – AbraCadaver Dec 04 '14 at 16:23
  • Will second example work? i just randomly made it up. Edit wow it does work i just forgot to assign sub function value to $new_text variable :) – Roman Toasov Dec 04 '14 at 16:25
  • possible duplicate of [Dynamic function calls in PHP](http://stackoverflow.com/questions/4556715/dynamic-function-calls-in-php) – Machavity Dec 04 '14 at 16:27

4 Answers4

1

You have it in your second example. Just make sure to check that it exists and then assign the return to the string. There is an assumption here about what the function accepts/requires as args and what it returns:

function do_stuff($text, $function='') {
    $new_text = nl2br($text);

    if(function_exists($function)) {
        $new_text = $function($new_text);
    }
    return $new_text;
}

$result = do_stuff("Hello \n World!", "strtoupper"); 
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • Thanks you were first. How to pass multiple arguments to function say if `$function="str_ireplace";` ? – Roman Toasov Dec 04 '14 at 16:45
  • Ended up using `call_user_func_array("function", $args_arr)` as it supports multiple arguments in any order e.g. i can do both `str_ireplace("find", "", $text)` and `stristr($text, "find")` with them having arguments in different order – Roman Toasov Dec 04 '14 at 16:58
1

Callables can be strings, arrays with a specific format, instances of the Closure class created using the function () {};-syntax and classes implementing __invoke directly. You can pass any of these to your function and call them using $myFunction($params) or call_user_func($myFunction, $params).

Additionally to the string examples already given in other answers you may also define a (new) function (closure). This might be especially beneficial if you only need the contained logic in one place and a core function is not suitable. You can also wrap paramters and pass additional values from the defining context that way:

Please be aware that the callable typehint requires php 5.4+

function yourFunction($text, callable $myFunction) { return $myFunction($text); }

$offset = 5;

echo yourFunction('Hello World', function($text) use($offset) {
    return substr($text, $offset);
});

Output: http://3v4l.org/CFMrI

Documentation hints to read on:

Rangad
  • 2,110
  • 23
  • 29
0

You can call a function like this:

$fcn = "strtoupper";
$fcn();

in the same way (as you found out yourself), you can have variable variables:

$a = "b";
$b = 4;
$$a;    // 4
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

Looks like you're almost there, just need to leave off the parentheses in the second parameter:

$result = do_stuff("Hello \n World!", "strtolower");

Then this should work after a little cleanup:

function do_stuff($text, $sub_function='') {
   $new_text = nl2br($text);

   if ($sub_function) {
      $new_text = $sub_function($new_text);
   }

   return $new_text;
}
mopo922
  • 6,293
  • 3
  • 28
  • 31