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?
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.
I hope someone can help me.
Cheers,
Robert
Hi!
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_absI 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/
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?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_absI know that GMP has overloaded operators so it doesn't seem so far that overloading functions is possible as well.
Yes, it is possible and you can see an example in /ext/mbstring's RINIT
function. It just loops through and replaces the entries in the function
table. No real magic there.
Note though that this is pretty ugly and since it is in the RINIT it
means it will happen on every single request regardless of whether any
of these functions are called during the request. As a quick local hack
you can do it though.
-Rasmus