Re: function overloading
| From: | Andrea Faulds | Date: | Wed, 03 Dec 2014 21:55:28 +0000 |
| Subject: | Re: function overloading | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-79403@lists.php.net to get a copy of this message | ||
Hi!
> On 3 Dec 2014, at 21:40, Robert Stoll <php@tutteli.ch> wrote:
>
> Heya,
>
>
>
> I would like to know If it is somehow possible to overload existing functions by extensions.
> And if it is possible, are
> there already some extension doing it?
Possibly in an extension, maybe. But as a weakly-typed, dynamic, reflective language, overloading of
functions is both a bad idea (unpredictable program execution) and unnecessary (types can be checked
at runtime, optional parameters exist). I also don't think overloading of functions is a good
idea generally, it leads to horrible APIs where there are 28 different versions of a function each
taking differently-typed arguments in different orders. I am glad PHP largely lacks this madness, it
makes it an easier language to read and to write. Overloading isn't needed: use optional
parameters, a named options associative array, or check argument types within the function. If what
you're doing doesn't fit into those categories, maybe you need to make a separate
function.
> I am not talking about the magic __call function. I am talking about something like: let's
> assume GMP has overloaded the
> function "abs" and therefore one can write:
> $n = gmp_init(-12345678901234567890);
> abs($n+ 2); // instead of using gmp_abs
>
>
>
> I know that GMP has overloaded operators so it doesn't seem so far that overloading
> functions is possible as well.
Being able to change APIs provided by other libraries from your library sounds like a bad idea.
If you just want abs() to work on bignums, my Big Integer Support RFC would handle that. :)
--
Andrea Faulds
http://ajf.me/