Req #73437 [NEW]: Feature request: function_alias()
From: oliver dot saunders at gmail dot com
Operating system:
PHP version: 7.1.0RC5
Package: Reflection related
Bug Type: Feature/Change Request
Bug description:Feature request: function_alias()
Description:
------------
(I choose "Reflection related" as "Package affected" because that was
the closest thing I could see but this really more of a core language
thing.)
PHP already has a function to create class aliases via class_alias():
class Foo { }
class_alias('Foo', 'Bar');
$x = new Bar;
var_dump($x); // -> object(Foo)#1 (0) { }
despite the relative ease with which a lot of classes can be aliased
using inheritance anyway:
class Foo { }
class Bar extends Foo { }
$x = new Bar; // has all features of Foo
(Granted inheritance isn't exactly the same as aliasing but functionally
equivalent in many cases.)
By comparison creating two functions of the same name requires you to
repeat the argument list and can potentially make mistakes in the
function body that would not be caught until the secondary function is
called:
function addThree($a, $b, $c) { return $a + $b + $c; }
function add3($a, $b, $c) {
return addThee($a, $b, $c); // mistake not caught until add3
called
}
A predefined function such as `function_alias(string $orig, string
$alias)` would simplify the above to:
function addThree($a, $b, $c) { return $a + $b + $c; }
function_alias('addThree', 'add3');
and allow errors to be caught earlier:
function_alias('addThee', 'add3'); // trigger error immediately
Additionally if the argument list of addThree were to change with
function_alias() it only has to be changed in one place instead of 2.
I program in PHP using a modern functional style and make liberal use of
freestanding functions i.e. not class members. To be able to simply
alias them would be very useful and convenient to anyone who likes to
program in this style which I believe will become more popular.
--
Edit bug report at https://bugs.php.net/bug.php?id=73437&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=73437&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=73437&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=73437&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=73437&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=73437&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=73437&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=73437&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=73437&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=73437&r=support
Expected behavior: https://bugs.php.net/fix.php?id=73437&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=73437&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=73437&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=73437&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73437&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=73437&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=73437&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=73437&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=73437&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=73437&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=73437&r=mysqlcfg
Thread (6 messages)
- oliver dot saunders at gmail dot com