如何在 PHP 中给函数取别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1688711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to alias a function in PHP?
提问by Atif Mohammed Ameenuddin
Is it possible to alias a function with a different name in PHP? Suppose we have a function with the name sleep. Is there a way to make an alias called wait?
是否可以在 PHP 中使用不同名称的函数别名?假设我们有一个名为 的函数sleep。有没有办法创建一个名为 的别名wait?
By now I'm doing like this:
到目前为止,我正在这样做:
function wait( $seconds ) {
sleep($seconds);
}
采纳答案by Jon
PHP 5.6+ only
仅 PHP 5.6+
Starting with PHP 5.6 it is possibleto alias a function by importing it:
从 PHP 5.6 开始,可以通过导入函数为函数取别名:
use function sleep as wait;
回答by Lukman
Until PHP 5.5
直到 PHP 5.5
yup, function wait ($seconds) { sleep($seconds); }is the way to go. But if you are worried about having to change wait() should you change the number of parameters for sleep() then you might want to do the following instead:
function wait ($seconds) { sleep($seconds); }是的,是要走的路。但是,如果您担心必须更改 wait() 是否应该更改 sleep() 的参数数量,那么您可能需要执行以下操作:
function wait() {
return call_user_func_array("sleep", func_get_args());
}
回答by Federico klez Culloca
Nope, but you can do this:
不,但你可以这样做:
$wait = 'sleep';
$wait($seconds);
This way you also resolve arguments-number-issues
通过这种方式,您还可以解决参数编号问题
回答by ólafur Waage
You can look at lambdas alsoif you have PHP 5.3
如果您有 PHP 5.3,您也可以查看lambdas
$wait = function($v) { return sleep($v); };
回答by Nathan Crause
If you aren't concerned with using PHP's "eval" instruction (which a lot of folks have a real problem with, but I do not), then you can use something like this:
如果您不关心使用 PHP 的“eval”指令(很多人对此有真正的问题,但我不关心),那么您可以使用以下内容:
function func_alias($target, $original) {
eval("function $target() { $args = func_get_args(); return call_user_func_array('$original', $args); }");
}
I used it in some simpletests, and it seemed to work fairly well. Here is an example:
我在一些简单的测试中使用了它,它似乎工作得很好。下面是一个例子:
function hello($recipient) {
echo "Hello, $recipient\n";
}
function helloMars() {
hello('Mars');
}
func_alias('greeting', 'hello');
func_alias('greetingMars', 'helloMars');
greeting('World');
greetingMars();
回答by Alan Storm
No, there's no quick way to do this in PHP. The language does not offer the ability to alias functions without writing a wrapper function.
不,在 PHP 中没有快速的方法来做到这一点。该语言不提供在不编写包装函数的情况下为函数设置别名的能力。
If you really really really needed this, you could write a PHP extensionthat would do this for you. However, to use the extension you'd need to compile your extension and configure PHP to us this extension, which means the portability of your application would be greatly reduced.
如果你真的真的真的需要这个,你可以编写一个 PHP 扩展来为你做这件事。但是,要使用该扩展,您需要编译您的扩展并将 PHP 配置给我们这个扩展,这意味着您的应用程序的可移植性将大大降低。
回答by Greg
No, functions aren't 1st-class citizens so there's no wait = sleeplike Javascript for example. You basically have to do what you put in your question:
不,函数不是一等公民,所以没有wait = sleep像 Javascript 这样的例子。你基本上必须做你在问题中提出的问题:
function wait ($seconds) { sleep($seconds); }
回答by user187291
you can use runkit extension
您可以使用 runkit 扩展
http://us.php.net/manual/en/function.runkit-function-copy.php
http://us.php.net/manual/en/function.runkit-function-copy.php
回答by sam
function alias($function)
{
return function (/* *args */) use ($function){
return call_user_func_array( $function, func_get_args() );
};
}
$uppercase = alias('strtoupper');
$wait = alias('sleep');
echo $uppercase('hello!'); // -> 'HELLO!'
$wait(1); // -> …
回答by kenorb
If your PHP doesn't support use x as ysyntax, in older PHP version you can define anonymous function:
如果您的 PHP 不支持use x as y语法,则在较旧的 PHP 版本中,您可以定义匿名函数:
$wait = create_function('$seconds', 'sleep($seconds);');
$wait(1);
Or place the code inside the constant, e.g.:
或者将代码放在常量中,例如:
define('wait', 'sleep(1);');
eval(wait);
See also: What can I use instead of eval()?
另请参阅:我可以使用什么来代替 eval()?
This is especially useful if you've long piece of code, and you don't want to repeat it or the code is not useful for a new function either.
如果您有一段很长的代码,并且您不想重复它,或者该代码对新函数也没有用处,这将特别有用。
There is also function posted by Dave Hwhich is very useful for creating an alias of a user function:
Dave H也发布了一个函数,它对于创建用户函数的别名非常有用:
function create_function_alias($function_name, $alias_name)
{
if(function_exists($alias_name))
return false;
$rf = new ReflectionFunction($function_name);
$fproto = $alias_name.'(';
$fcall = $function_name.'(';
$need_comma = false;
foreach($rf->getParameters() as $param)
{
if($need_comma)
{
$fproto .= ',';
$fcall .= ',';
}
$fproto .= '$'.$param->getName();
$fcall .= '$'.$param->getName();
if($param->isOptional() && $param->isDefaultValueAvailable())
{
$val = $param->getDefaultValue();
if(is_string($val))
$val = "'$val'";
$fproto .= ' = '.$val;
}
$need_comma = true;
}
$fproto .= ')';
$fcall .= ')';
$f = "function $fproto".PHP_EOL;
$f .= '{return '.$fcall.';}';
eval($f);
return true;
}

