Req #73437 [Opn]: Feature request: function_alias()

From: Date: Tue, 01 Nov 2016 21:17:33 +0000
Subject: Req #73437 [Opn]: Feature request: function_alias()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-205131@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73437&edit=1

 ID:                 73437
 Updated by:         yohgaki@php.net
 Reported by:        oliver dot saunders at gmail dot com
 Summary:            Feature request: function_alias()
 Status:             Open
 Type:               Feature/Change Request
 Package:            Reflection related
 PHP Version:        7.1.0RC5
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2016-11-01 20:56:51] 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 20:48:40] 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:12:58] 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.



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=73437&edit=1


Thread (6 messages)

« previous php.bugs (#205131) next »