-5

Possible Duplicate:
Use a variable to define a PHP function
Use Variable as Function Name in PHP

I want to perform a conditional function call but I don't necessarily know what what the function will be, so that would be a long switch.

For example;

$userSelection = "calculator"; /* or "stocks" or whatever widget */

$widget->get_widget($userSelection);

public function __construct($userSelection){

/* pseudo code */
call function $userSelection();

}

public function calculator(){
/* Get Calculator */
}
Community
  • 1
  • 1
James Vince
  • 1,269
  • 3
  • 10
  • 12
  • 2
    Searching "PHP variable function" in google brings immediately this result: [PHP variable functions](http://php.net/manual/en/functions.variable-functions.php) – Jocelyn Aug 01 '12 at 10:36

9 Answers9

1

Sure there is. This feature is called variable functions:

$functionName = "strlen";
$length = $$functionName("Hello world!");

The $$var(...) syntax is convenient, but it will only work with free functions. If you want to call a class method this way, you will need to use call_user_func or call_user_func_array (these functions can also handle the "free function" case).

Jon
  • 428,835
  • 81
  • 738
  • 806
0

Look at the call-user-func function. This allows you to call another function, e.g.

call_user_func('calculator')
borrible
  • 17,120
  • 7
  • 53
  • 75
0

call_user_func($userSelection);

http://php.net/manual/en/function.call-user-func.php

Lee
  • 10,496
  • 4
  • 37
  • 45
0

Take a look at this php functions:

call_user_func(): http://php.net/manual/de/function.call-user-func.php

call_user_func_array(): http://www.php.net/manual/de/function.call-user-func-array.php

create_function(): http://www.php.net/manual/de/function.create-function.php

There is also a direct (though ugly) execution syntax:

function some_func(args) {...}
$function_name='some_func';
$$function_name(args2);
arkascha
  • 41,620
  • 7
  • 58
  • 90
0

You can use call_user_func() for that, like this:

$userSelection = "calculator";
call_user_func($userSelection[, $param1, $param2, ...]);
call_user_func_array($userSelection, $params);
aorcsik
  • 15,271
  • 5
  • 39
  • 49
0

It's pretty simple if that's what you mean.

function calculator() {
    echo 'foo';
}

$userSelection = "calculator";

if (function_exists($userSelection)) {
    $userSelection();
}

Or within a class like in your example:

    class widget {

        public function __construct($userSelection) {
            echo 'constructed widget<br>';
            if (function_exists($userSelection)) {
                $this->$userSelection();
            }
        }

        public function calculator() {
            echo 'bar';
        }

    }

    $userSelection = "calculator";
    $widget = new widget($userSelection);

Or from outside a class when the function is part of the class.

    class widget {

        public function calculator() {
            echo 'bar';
        }

    }

    $widget = new widget();
    $userSelection = "calculator";
    $widget->$userSelection();

I would work with if/else statements though to determine the function to be called just to be sure that only valid functions are executed (do you sanitize the user selection or do you just get it from a $_POST? The latter would be a very bad idea).

MiDo
  • 1,057
  • 1
  • 7
  • 11
0

If it's just a function you're after then using this should solve your problems

$function = "echo";
$$function "fooBar";

If it's a class method that you want to keep flexible use magic method __call() which will allow you to use method names that are not pre-defined.

__call() is triggered when invoking inaccessible methods in an object context.

i.e.

class Foo {
    public function __call($name, $arguments) {
        echo $name;
    }
}

$foo = new Foo();
$foo->bar(); // will echo "bar"
Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
0

PHP built-in function 'eval' can do everything, but beware of injection.

$var = "somefunction";
eval("$var();");

http://php.net/manual/en/function.eval.php

jondinham
  • 8,271
  • 17
  • 80
  • 137
0

You can do following :

$var = 'abc';

switch ($var) {
  case 'abc':
    $result = $var('test param');
    echo $result;
  break;
 default :
    echo 'default';
 break;
}

function abc($data) {
  return $data;
}
Atul Rai
  • 242
  • 2
  • 10