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

3
  • I don't understand. What you're asking you have achieved in your second example. Commented Dec 4, 2014 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 :) Commented Dec 4, 2014 at 16:25
  • possible duplicate of Dynamic function calls in PHP Commented Dec 4, 2014 at 16:27

4 Answers 4

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

2 Comments

Thanks you were first. How to pass multiple arguments to function say if $function="str_ireplace"; ?
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
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:

Comments

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

Comments

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

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.