347

I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:

function foo ()
{
    //code here
}

function bar ()
{
    //code here
}

$functionName = "foo";
// I need to call the function based on what is $functionName
Rafid Aslam
  • 305
  • 3
  • 10
Lizard
  • 43,732
  • 39
  • 106
  • 167
  • I know this was almost 2 years ago, but thank you for asking this. Made my life so much easier. – jpgerb Dec 29 '22 at 16:47

18 Answers18

531

$functionName() or call_user_func($functionName)

If you need to provide parameters stored in another variable (in the form of array), use array unpacking operator:

$function_name = 'trim';
$parameters = ['aaabbb','b'];
echo $function_name(...$parameters); // aaa

To dynamically create an object and call its method use

$class = 'DateTime';
$method = 'format';
echo (new $class)->$method('d-m-Y');

or to call a static method

$class = 'DateTime';
$static = 'createFromFormat';
$date = $class::$static('d-m-Y', '17-08-2023');
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 101
    If you need to call an object's function whose name is in a variable do this: call_user_func( array($obj,$func), $params ) – DivinesLight Nov 18 '11 at 11:38
  • 1
    Also see my answer below if you are dealing with classes and methods. I didn't expect what I found. – Krista K Jan 28 '13 at 23:16
  • How could I void function definition error if the the function was not defined? Does if($functionName) enough? – SaidbakR May 02 '13 at 09:27
  • 16
    @sємsєм: You should use the [`is_callable`](http://php.net/is_callable) function. It will check if a variable contains something that can be called as a function (be it the name of a function, an array containing an object instance and a function name, or an anonymous function/closure) – knittl May 02 '13 at 17:36
  • @BlackDivine, what if it's a static method? – Pacerier Jul 30 '13 at 10:33
  • 11
    @Pacerier: Provide the classname as first element of the array: `call_user_func(array('ClassName', 'method'), $args)` – knittl Jul 30 '13 at 10:49
  • @DavidSpector: so `call_user_func(['U', 'name'], $args)` does not work for you? Do you get any errors? (make sure to enable error_reporting and display_errors) – knittl Jan 18 '20 at 15:42
  • ``call_user_func([__CLASS__, $functionName], ...$args)`` if the function is in current Class. – Aldo Oct 15 '21 at 15:33
  • I've been doing this the 'hard' way for years -- switch statement to figure out the value and call the appropriate function. This is SOOOOO much easier. – jpgerb Dec 29 '22 at 16:46
159

My favorite version is the inline version:

${"variableName"} = 12;

$className->{"propertyName"};
$className->{"methodName"}();

StaticClass::${"propertyName"};
StaticClass::{"methodName"}();

You can place variables or expressions inside the brackets too!

Lajos Mészáros
  • 3,756
  • 2
  • 20
  • 26
  • 1
    @marcioAlmada : post a comment instead of editing others' answers in this manner; it adds confusion - it's not clear who said what *[to the curious: see revisions for explanations]* – kryger Feb 02 '14 at 16:08
  • 5
    Ok @kryger. Line 2 `{"functionName"}();` is not legal and will throw a syntax error. – marcio Feb 02 '14 at 23:53
  • 4
    Thank you guys, for the replies, it indeed throws an error, even in php 5.4, so that syntax only works with object methods. Edited the post. – Lajos Mészáros Feb 03 '14 at 10:10
  • 1
    Note that generally you can combine `Variable functions` and `Variable variables` so you can actually do something like `StaticClass::${StaticClass::$method_name}()` or `self::{self::$some_method}()` :) seems weird, but it's useful when you want to have e.g. a variable loading function in `__call` :) – jave.web Feb 25 '21 at 01:14
45

Solution: Use PHP7

Note: For a summarized version, see TL;DR at the end of the answer.

Old Methods

Update: One of the old methods explained here has been removed. Refer to other answers for explanation on other methods, it isn't covered here. By the way, if this answer doesn't help you, you should return upgrading your stuff. PHP 5.6 support has ended in January 2019 (now even PHP 7.2 and 7.3 are not being supported). See supported versions for more information.

As others mentioned, in PHP5 (and also in newer versions like PHP7) we could use variables as function names, use call_user_func() and call_user_func_array(), etc.

New Methods

As of PHP7, there are new ways introduced:

Note: Everything inside <something> brackets means one or more expressions to form something, e.g. <function_name> means expressions forming a function name.

Dynamic Function Call: Function Name On-the-fly

We can form a function name inside parentheses in just one go:

(<function_name>)(arguments);

For example:

function something(): string
{
    return "something";
}

$bar = "some_thing";

(str_replace("_", "", $bar))(); // something

// Possible, too; but generally, not recommended, because makes your
// code more complicated
(str_replace("_", "", $bar))()(); 

Note: Although removing the parentheses around str_replace() is not an error, putting parentheses makes code more readable. However, you cannot do that sometimes, e.g. while using . operator. To be consistent, I recommend you to put the parentheses always.

Dynamic Function Call: Callable Property

A useful example would be in the context of objects: If you have stored a callable in a property, you have to call it this way:

($object->{<property_name>})();

As a simple example:

// Suppose we're in a class method context
($this->eventHandler)();

Obviously, calling it as $this->eventHandler() is plain wrong: By that you mean calling a method named eventHandler.

Dynamic Method Call: Method Name On-the-fly

Just like dynamic function calls, we can do the same way with method calls, surrounded by curly braces instead of parentheses (for extra forms, navigate to TL;DR section):

$object->{<method_name>}(arguments);
$object::{<method_name>}(arguments);

See it in an example:

class Foo
{
    public function another(): string
    {
        return "something";
    }
}

$bar = "another thing";

(new Something())->{explode(" ", $bar)[0]}(); // something

Dynamic Method Call: The Array Syntax

A more elegant way added in PHP7 is the following:

[<object>, <method_name>](arguments);
[<class_name>, <method_name>](arguments); // Static calls only

As an example:

class Foo
{
    public function nonStaticCall()
    {
        echo "Non-static call";
    }
    public static function staticCall()
    {
        echo "Static call";
    }
}

$x = new Foo();

[$x, "non" . "StaticCall"](); // Non-static call
[$x, "static" . "Call"](); // Static call

Note: The benefit of using this method over the previous one is that, you don't care about the call type (i.e. whether it's static or not).

Note: If you care about performance (and micro-optimizations), don't use this method. As I tested, this method is really slower than other methods (more than 10 times).

Extra Example: Using Anonymous Classes

Making things a bit complicated, you could use a combination of anonymous classes and the features above:

$bar = "SomeThing";

echo (new class {
    public function something()
    {
        return 512;
    }
})->{strtolower($bar)}(); // 512

TL;DR (Conclusion)

Generally, in PHP7, using the following forms are all possible:

// Everything inside `<something>` brackets means one or more expressions
// to form something

// Dynamic function call via function name
(<function_name>)(arguments);

// Dynamic function call on a callable property
($object->{<property_name>})(arguments);

// Dynamic method call on an object
$object->{<method_name>}(arguments);
$object::{<method_name>}(arguments);

// Dynamic method call on a dynamically-generated object
(<object>)->{<method_name>}(arguments);
(<object>)::{<method_name>}(arguments);

// Dynamic method call, statically
ClassName::{<method_name>}(arguments);
(<class_name>)::{<method_name>}(arguments);

// Dynamic method call, array-like (no different between static
// and non-static calls
[<object>, <method_name>](arguments);

// Dynamic method call, array-like, statically
[<class_name>, <method_name>](arguments);

Special thanks to this PHP talk.

MAChitgarha
  • 3,728
  • 2
  • 33
  • 40
24

Yes, it is possible:

function foo($msg) {
    echo $msg."<br />";
}
$var1 = "foo";
$var1("testing 1,2,3");

Source: http://www.onlamp.com/pub/a/php/2001/05/17/php_foundations.html?page=2

Community
  • 1
  • 1
Sev
  • 15,401
  • 9
  • 56
  • 75
20

As already mentioned, there are a few ways to achieve this with possibly the safest method being call_user_func() or if you must you can also go down the route of $function_name(). It is possible to pass arguments using both of these methods as so

$function_name = 'foobar';

$function_name(arg1, arg2);

call_user_func_array($function_name, array(arg1, arg2));

If the function you are calling belongs to an object you can still use either of these

$object->$function_name(arg1, arg2);

call_user_func_array(array($object, $function_name), array(arg1, arg2));

However if you are going to use the $function_name() method it may be a good idea to test for the existence of the function if the name is in any way dynamic

if(method_exists($object, $function_name))
{
    $object->$function_name(arg1, arg2);
}
olliefinn
  • 587
  • 4
  • 16
15

A few years late, but this is the best manner now imho:

$x = (new ReflectionFunction("foo"))->getClosure();
$x();
13

In case someone else is brought here by google because they were trying to use a variable for a method within a class, the below is a code sample which will actually work. None of the above worked for my situation. The key difference is the & in the declaration of $c = & new... and &$c being passed in call_user_func.

My specific case is when implementing someone's code having to do with colors and two member methods lighten() and darken() from the csscolor.php class. For whatever reason, I wanted to have the same code be able to call lighten or darken rather than select it out with logic. This may be the result of my stubbornness to not just use if-else or to change the code calling this method.

$lightdark="lighten"; // or optionally can be darken
$color="fcc";   // a hex color
$percent=0.15;  
include_once("csscolor.php");
$c = & new CSS_Color($color);
$rtn=call_user_func( array(&$c,$lightdark),$color,$percent);

Note that trying anything with $c->{...} didn't work. Upon perusing the reader-contributed content at the bottom of php.net's page on call_user_func, I was able to piece together the above. Also, note that $params as an array didn't work for me:

// This doesn't work:
$params=Array($color,$percent);
$rtn=call_user_func( array(&$c,$lightdark),$params);

This above attempt would give a warning about the method expecting a 2nd argument (percent).

Krista K
  • 21,503
  • 3
  • 31
  • 43
  • what does "$c = & new CSS_Color" do? I'm talking about the amp (&). What does this & change? – Danon Oct 09 '15 at 11:01
  • @danon & is "address of" or the "reference" operator. I am instiantiating a new object of class CSS_Color and then assigning its reference (aka address) to $c. I hate repeating code, so I often use variable variable names. – Krista K Oct 11 '15 at 13:08
  • 1
    I don't understand. Aren't objects passed by reference everywhere anyways? I mean, if you pass a $c to a function and change it, the original object will be affected too, right? No matter whether you used this & or not, am I right? – Danon Oct 11 '15 at 21:27
  • I tried different combinations of symbols and only while reading the user comments on php.org was I able to get working code. You'll need to start that discussion with people responsible for php's behavior. – Krista K Oct 12 '15 at 23:59
  • For the second example you misread, you were looking for call_user_func_array – Tofandel Mar 03 '19 at 00:28
10

For the sake of completeness, you can also use eval():

$functionName = "foo()";
eval($functionName);

However, call_user_func() is the proper way.

Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
karim79
  • 339,989
  • 67
  • 413
  • 406
  • 8
    It should be noted that eval() will allow the same behaviour but it would also allow to execute any PHP code. So the variable could be used to hijack the system. The call_user_func() does not bring such security issues. – John Oct 04 '14 at 02:19
  • 4
    `eval` is not a solution to this question. – ankr Oct 17 '14 at 09:21
  • 2
    Please don't do this because an attacker could wipe out your server entirely. – andreszs Jul 11 '18 at 15:16
8

Dynamic function names and namespaces

Just to add a point about dynamic function names when using namespaces.

If you're using namespaces, the following won't work except if your function is in the global namespace:

namespace greetings;

function hello()
{
    // do something
}

$myvar = "hello";
$myvar(); // interpreted as "\hello();"

What to do?

You have to use call_user_func() instead:

// if hello() is in the current namespace
call_user_func(__NAMESPACE__.'\\'.$myvar);

// if hello() is in another namespace
call_user_func('mynamespace\\'.$myvar);
Jivan
  • 21,522
  • 15
  • 80
  • 131
5

Complementing the answer of @Chris K if you want to call an object's method, you can call it using a single variable with the help of a closure:

function get_method($object, $method){
    return function() use($object, $method){
        $args = func_get_args();
        return call_user_func_array(array($object, $method), $args);           
    };
}

class test{        

    function echo_this($text){
        echo $text;
    }
}

$test = new test();
$echo = get_method($test, 'echo_this');
$echo('Hello');  //Output is "Hello"

I posted another example here

Community
  • 1
  • 1
David
  • 2,942
  • 33
  • 16
4

Use the call_user_func function.

pauljwilliams
  • 19,079
  • 3
  • 51
  • 79
4

What I learnt from this question and the answers. Thanks all!

Let say I have these variables and functions:

$functionName1 = "sayHello";
$functionName2 = "sayHelloTo";
$functionName3 = "saySomethingTo";

$friend = "John";
$datas = array(
    "something"=>"how are you?",
    "to"=>"Sarah"
);

function sayHello()
{
echo "Hello!";
}

function sayHelloTo($to)
{
echo "Dear $to, hello!";
}

function saySomethingTo($something, $to)
{
echo "Dear $to, $something";
}
  1. To call function without arguments

    // Calling sayHello()
    call_user_func($functionName1); 
    

    Hello!

  2. To call function with 1 argument

    // Calling sayHelloTo("John")
    call_user_func($functionName2, $friend);
    

    Dear John, hello!

  3. To call function with 1 or more arguments This will be useful if you are dynamically calling your functions and each function have different number of arguments. This is my case that I have been looking for (and solved). call_user_func_array is the key

    // You can add your arguments
    // 1. statically by hard-code, 
    $arguments[0] = "how are you?"; // my $something
    $arguments[1] = "Sarah"; // my $to
    
    // 2. OR dynamically using foreach
    $arguments = NULL;
    foreach($datas as $data) 
    {
        $arguments[] = $data;
    }
    
    // Calling saySomethingTo("how are you?", "Sarah")
    call_user_func_array($functionName3, $arguments);
    

    Dear Sarah, how are you?

Yay bye!

asmasyakirah
  • 115
  • 3
  • 11
4

If you were in a object context trying to call a function dynamically please try something like this code bellow:

$this->{$variable}();
Rafael Xavier
  • 956
  • 13
  • 13
3

The easiest way to call a function safely using the name stored in a variable is,

//I want to call method deploy that is stored in functionname 
$functionname = 'deploy';

$retVal = {$functionname}('parameters');

I have used like below to create migration tables in Laravel dynamically,

foreach(App\Test::$columns as $name => $column){
        $table->{$column[0]}($name);
}
Debashis Prusty
  • 393
  • 2
  • 6
2

Following code can help to write dynamic function in PHP. now the function name can be dynamically change by variable '$current_page'.

$current_page = 'home_page';
$function = @${$current_page . '_page_versions'};
$function = function() {
    echo 'current page';
};
$function();
rakesh.falke
  • 121
  • 3
  • 1
    [Anonymous functions](https://www.php.net/manual/en/functions.anonymous.php) (i.e. dynamic functions) differ from [variable functions](https://www.php.net/manual/en/functions.variable-functions.php) (i.e. calling functions dynamically). Also, you are replacing `$function` variable without any reasons. – MAChitgarha Apr 07 '20 at 11:31
1

Considering some of the excellent answers given here, sometimes you need to be precise. For example.

  1. if a function has a return value eg (boolean,array,string,int,float e.t.c).
  2. if the function has no return value check
  3. if the function exists

Let's look at its credit to some of the answers given.

Class Cars{
        function carMake(){
        return 'Toyota';
        }
        function carMakeYear(){
        return 2020;
        }
        function estimatedPriceInDollar{
        return 1500.89;
        }
        function colorList(){
        return array("Black","Gold","Silver","Blue");
        }
        function carUsage(){
        return array("Private","Commercial","Government");
        }
    function getCar(){
    echo "Toyota Venza 2020 model private estimated price is 1500 USD";
    }
 }

We want to check if method exists and call it dynamically.

$method = "color List";
        $class = new Cars();
        //If the function have return value;
        $arrayColor = method_exists($class, str_replace(' ', "", $method)) ? call_user_func(array($this, $obj)) : [];
        //If the function have no return value e.g echo,die,print e.t.c
     $method = "get Car";
        if(method_exists($class, str_replace(' ', "", $method))){
        call_user_func(array($class, $method))
        }

Thanks

Sirjiskit
  • 431
  • 5
  • 16
  • I don't see what value this answer adds to this thread. Please explain the context of `$this` in your example and the variable `$obj` for the method name. I think we know what you intend there ($class and reassignment of $method) but that's not what you said. Also, please fix your formatting. I would do it but not if you're going to modify the code. Thx. – TonyG Dec 21 '21 at 17:48
-5

One unconventional approach, that came to my mind is, unless you are generating the whole code through some super ultra autonomous AI which writes itself, there are high chances that the functions which you want to "dynamically" call, are already defined in your code base. So why not just check for the string and do the infamous ifelse dance to summon the ...you get my point.

eg.

if($functionName == 'foo'){
  foo();
} else if($functionName == 'bar'){
  bar();
}

Even switch-case can be used if you don't like the bland taste of ifelse ladder.

I understand that there are cases where the "dynamically calling the function" would be an absolute necessity (Like some recursive logic which modifies itself). But most of the everyday trivial use-cases can just be dodged.

It weeds out a lot of uncertainty from your application, while giving you a chance to execute a fallback function if the string doesn't match any of the available functions' definition. IMHO.

Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
  • If the value of the variable is already the name of the function why do the extra work? also it would be good to get solid feedback to see if the function exists by php before you run it: `if (method_exists($functionName)) $functionName(); else //exception or handle` – Necro Dec 03 '18 at 00:38
  • 2
    Yup your point is valid, but since the function name is dynamic, in your logic any string that comes shall be executed if a function by that name exists, irrespective of if that function is relevant in current situation or not. My approach makes sure that only a certain bunch of functions can be executed else an error can be thrown. It can be simplified by arrays and as such. – Mohd Abdul Mujib Dec 03 '18 at 02:36
  • @MohdAbdulMujib, so then validating it using `in_array()` or even regex is a better approach IMO. – MAChitgarha Aug 09 '22 at 08:47
-10

I dont know why u have to use that, doesnt sound so good to me at all, but if there are only a small amount of functions, you could use a if/elseif construct. I dont know if a direct solution is possible.

something like $foo = "bar"; $test = "foo"; echo $$test;

should return bar, you can try around but i dont think this will work for functions

Flo
  • 1,445
  • 2
  • 10
  • 12