[英]Dynamic function calls in PHP
Is it possible to have dynamic function calls in PHP? 是否可以在PHP中进行动态函数调用? I don't know if I am calling it the right name, but my example hopefully will explain what I want.
我不知道我是否称它为正确的名字,但我的例子希望能解释我想要的东西。
<?
function image_filename(){
global $the_image;
return return $the_image->filename;
}
function image_anchor(){
global $the_image;
return getAnchor($the_image->id);
}
// is there a way to make a function that will do something like this:
// I know it's possible using a class and __call, but is it possible for a general case
function image_REQUEST(){
global $the_image;
$args = func_get_args();
switch(REQUEST){
case "filename":
return $the_image->filename;
break;
case "anchor":
return getAnchor($the_image->id);
break;
}
}
?>
Clarification: 澄清:
I know about variable functions, and call_user_func
. 我知道变量函数和
call_user_func
。 These are not what I am looking for. 这些不是我想要的。 Basically, I don't want to define
image_filename
or image_anchor
, but have them defined when they are called. 基本上,我不想定义
image_filename
或image_anchor
,而是在调用它们时定义它们。
There is no way to define "magic" or "dynamic" functions (like with __call) for functions not defined within a class. 没有办法来定义一个类中没有定义的功能“魔力”或“动态”的功能(如使用__call)。 You can however call functions dynamically.
但是,您可以动态调用函数。 There are several ways to do this- I would recommend call_user_func_array() function, which lets you call a function, passing its arguments as an array.
有几种方法可以做到这一点 - 我建议使用call_user_func_array()函数,它允许你调用一个函数,将它的参数作为数组传递。
For example: 例如:
$type = 'filename';
call_user_func_array("image_$type", $args);
For more info, see http://php.net/manual/en/function.call-user-func-array.php 有关详细信息,请参阅http://php.net/manual/en/function.call-user-func-array.php
You mean variable functions? 你的意思是变量函数?
<?php
function user_func($x) { echo $x; }
$x = "user_func";
$x(1);
?>
http://php.net/manual/en/functions.variable-functions.php http://php.net/manual/en/functions.variable-functions.php
To dynamically create functions, use create_function
(http://ca3.php.net/create-function): 要动态创建函数,请使用
create_function
(http://ca3.php.net/create-function):
<?php
$func = create_function('$x', 'echo $x;');
$func(1);
?>
You can store them in arrays: 您可以将它们存储在数组中:
<?php
$funcs = array();
$funcs['error'] = create_function('$x', 'echo $x;');
?>
Yes. 是。
But don't. 但不要。 unless you like having future programmers hunt you down and gut you on the sidewalk.
除非你喜欢让未来的程序员追捕你,然后把你放在人行道上。
Use something like: 使用类似的东西:
$image_request = 'image_' + REQUEST;
echo $image_request();
// lets say REQUEST = filename, then above will echo the result of function: image_filename();
These are known as variable functions , and the basic is that you store the function's name in a variable, lets say $var
, and then call the function using: $var()
. 这些被称为变量函数 ,基本是将函数的名称存储在变量中,比如
$var
,然后使用: $var()
调用函数。
Also, as you state in your comment to BoltClock, if you are interested in a kind of a dynamic function set, why not use something like this: 另外,正如您在对BoltClock的评论中所述,如果您对某种动态函数集感兴趣,为什么不使用这样的东西:
function image_functions(REQUEST) {
switch (REQUEST) {
// ...
}
}
Is this what you are looking for ? 这是你想要的 ?
function foo() {
// code ...
}
$functionName = "foo";
$functionName();
http://php.net/manual/en/function.call-user-func.php http://php.net/manual/en/function.call-user-func.php
<?php
function increment(&$var)
{
$var++;
}
function decrement(&$var)
{
$var--;
}
$a = 5;
$func = "increment";
call_user_func($func, $a);
echo $a."\n";
$func = "decrement";
call_user_func($func, $a);
call_user_func($func, $a);
echo $a."\n";;
?>
produces 产生
6
4
You can also do simple string replacements. 您也可以进行简单的字符串替换。
<?php
$action = $_REQUEST['action'];
$functionName = 'image_' . $action;
if (function_exists($functionName)) {
$functionName($the_image);
} else {
echo "That function is not available";
}
And that's about it. 这就是它。 I added the extra error checking so that you don't try to run functions that don't exist.
我添加了额外的错误检查,以便您不要尝试运行不存在的函数。
I don't understand why you don't just make REQUEST
a parameter but you can define functions inside an eval()
call. 我不明白为什么你不只是让
REQUEST
参数,但你可以在eval()
调用中定义函数。
function makeFunction($name)
{
$functionName = "process_{$name}";
eval("
function {$functionName}() {
echo \"Hi, this is {$functionName}.\\n\";
}
");
}
makeFunction("Hello");
process_Hello();
Good luck with the escapes. 好运逃脱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.