Reputation: 9496
PHP has an interesting feature where you can use count()
or sizeof()
to see length of an array. Similarly you can either use either join
or implode
, to make an array into a string. It gives you the flexibility to use either strchr
or strstr
, chop
or rtrim
, exit
or die
, is_integer
or is_int
, etc. to do the exact same operation.
Are there any examples of this aliasing in other common programming languages such as Java or C, or Python? And what exactly is the purpose of such a feature?
The closest thing I can think of is how Python lets you use import somemodule
vs from somemodule import a, b, c...
, or even do from math import sqrt as mysquarerootfunction
.
However, this has obvious advantages in Python because you could change a program that uses real square roots, to complex sqrt(), just by changing a from math import *
to from cmath import *
, for example.
Upvotes: 1
Views: 350
Reputation: 10868
Most languages and APIs with a long history have at least some aliases, in the sense of two ways to do the exact same thing using different names. However it is not at all common to have two different names for exactly the same operation. I have not been able to find any examples in the languages I use frequently (PHP is not one of them).
What is common is
1. An older way is replaced by a newer way and both remain available. The C function printf()
and C++ stream <<
are like that. Indeed most of the C standard library has been replaced by the C++ standard library, which means lots of opportunities to do the same thing two different ways. Java and .NET are full of examples too.
2. A simple function has a more complicated cousin, which can do all the simple things. The C functions atod() and strtof() are like that. There are plenty of functions in the Windows API with names like SomeFunction() and SomeFunctionEx().
To answer your other question, the only purpose is backward compatibility. Nobody in their right mind designs a language with two names for a single function. These things just happen as languages evolve, and PHP is quite old now.
Upvotes: 1
Reputation: 1166
There is an explanation about function aliases in the PHP Manual Appendix as well as a list of all functions with aliases:
There are quite a few functions in PHP which you can call with more than one name. In some cases there is no preferred name among the multiple ones, is_int() and is_integer() are equally good for example. However there are functions which changed names because of an API cleanup or some other reason and the old names are only kept as aliases for backward compatibility. It is usually a bad idea to use these kind of aliases, as they may be bound to obsolescence or renaming, which will lead to unportable script. This list is provided to help those who want to upgrade their old scripts to newer syntax.
Upvotes: 2