php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #73437 Feature request: function_alias()
Submitted: 2016-11-01 20:12 UTC Modified: 2016-11-08 22:45 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: oliver dot saunders at gmail dot com Assigned:
Status: Open Package: Reflection related
PHP Version: 7.1.0RC5 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: oliver dot saunders at gmail dot com
New email:
PHP Version: OS:

 

 [2016-11-01 20:12 UTC] oliver dot saunders at gmail dot com
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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-01 20:48 UTC] yohgaki@php.net
I would like to have this kind of feature, but not with alias functions. IMO, we should use namespace to handle userland class/function name aliasing.
 [2016-11-01 20:56 UTC] yohgaki@php.net
BTW, we can do function aliasing via namespace. I would like to have ability to import name into root namespace. i.e. Replace module defined names with user defined one. This feature can be used like "monkey patch" in ruby.
 [2016-11-01 21:17 UTC] yohgaki@php.net
We may be better to soft deprecate class_alias. i.e. Deprecation by document only.
http://php.net/manual/en/function.class-alias.php

Namespace can be used for aliasing and it's recommended way. The manual page should contain link to namespaced aliasing and recommend it rather than class_alias() at least.
 [2016-11-08 22:45 UTC] oliver dot saunders at gmail dot com
Please however note that namespace aliases are file-local while class_alias() and the hypothetical function_alias() are not. This is a significant limitation of namespace aliases.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 04:01:27 2019 UTC