PHP 5.6+ only

How would this look for methods of a class?
–
Nathan J. BrauerSep 27 '14 at 0:42

@NathanJ.Brauer: Are you asking how to call the method foo of an existing object using the name bar, calling a static method foo of a class using the name bar, or something else? For the two cases I mentioned, the short answer is you can't.
–
JonOct 10 '14 at 0:02

@NathanJ.Brauer: All of that assumes that you own the class and are free to make modifications. But there is no language feature that allows you to do this "from the outside", as with use (function) X as Y.
–
JonOct 10 '14 at 15:19

1

Correct - I want to be able to, in my own class, provide useful aliases to those who use the class. Sometimes this is a means of graceful deprecation (e.g. a former version of a framework had a misspelled method), while other times it's just nice UX.
–
Nathan J. BrauerOct 11 '14 at 2:27

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:

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);
–
MarkoNov 6 '09 at 18:57

This is my preferred answer because it also allows you to alias a static method inside of a class, like this: call_user_func_array(array("Class", "staticMethod"), func_get_args());
–
OCDevFeb 20 '12 at 17:35

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.