views:

1198

answers:

9

is it possible to alias a function with a different name in PHP? if yes how ?

suppose we have this function sleep();

is there any quick way to make an alias called wait();

without writing this code

function wait ($seconds)  {
    sleep($seconds);
}
+1  A: 

nope. the way you wrote is the best way to do it.

GSto
+1  A: 

No, there's no quick way to do so - at least for anything before PHP v5.3, and it's not a particularly good idea to do so either. It simply complicates matters.

Alister Bulman
+2  A: 

No, functions aren't 1st-class citizens so there's no wait = sleep like Javascript for example. You basically have to do what you put in your question:

function wait ($seconds) { sleep($seconds); }
Greg
+3  A: 

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.

If you really really really needed this, you could write a PHP extension that 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.

Alan Storm
You wouldn't necessarily have to recompile php. You could compile the module and include it via php.ini instead.
Kevin Peno
Very true Kevin, I've updated the post to reflect your comments
Alan Storm
+8  A: 

Nope, but you can do this:

$wait = 'sleep';
$wait($seconds);

This way you also resolve arguments-number-issues

klez
variable variables can get really confusing though. I don't think this is the best way to solve the problem here.
GSto
@GSto this wasn't meant to be the best solution. it was just a possibility. I can't believe it's been upvoted so much :-)
klez
@Ólafur Waage, thanks for the edit
klez
you're welcome, i got a syntax error in my brain :P
Ólafur Waage
+5  A: 

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() { 
  return call_user_func_array("sleep", func_get_args());
}
Lukman
`func_get_args()` will work like that only in PHP > 5.3 where you can use it in a parameter list. In PHP < 5.3 you have to use a temporary variable: `$args = func_get_args(); return call_user_unc_array('sleep', $args);`
Marko
+5  A: 

You can look at lambdas also if you have PHP 5.3

$wait = function($v) { return sleep($v); };
Ólafur Waage
+2  A: 

you can use runkit extension

http://us.php.net/manual/en/function.runkit-function-copy.php

stereofrog
A: 

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:

function func_alias($target, $original) {
    eval("function $target() { \$args = func_get_args(); return call_user_func_array('$original', \$args); }");
}

I used it in some simple tests, 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();
Nathan Crause