-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 */
}
4

9 回答 9

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).

于 2012-08-01T10:35:00.013 回答
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).

于 2012-08-01T10:37:07.660 回答
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"
于 2012-08-01T10:40:03.617 回答
0

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

call_user_func('calculator')
于 2012-08-01T10:35:02.600 回答
0

call_user_func($userSelection);

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

于 2012-08-01T10:35:35.703 回答
0

看看这个 php 函数:

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

还有一种直接(虽然丑陋)的执行语法:

function some_func(args) {...}
$function_name='some_func';
$$function_name(args2);
于 2012-08-01T10:35:51.137 回答
0

您可以使用call_user_func()它,如下所示:

$userSelection = "calculator";
call_user_func($userSelection[, $param1, $param2, ...]);
call_user_func_array($userSelection, $params);
于 2012-08-01T10:36:37.177 回答
0

PHP 内置函数 'eval' 可以做任何事情,但要小心注入。

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

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

于 2012-08-01T10:41:30.647 回答
0

您可以执行以下操作:

$var = 'abc';

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

function abc($data) {
  return $data;
}
于 2012-08-01T10:45:44.227 回答