views:

1828

answers:

9

It seems that there is no real pattern to the way functions are named, str_replace, strrpos, strip_tags, stripslashes are just some.

Why is this the case?

EDIT - this wasn't meant as a "troll" type post - just something that I think everytime I use the language!

+1  A: 

For backwards compatibility over a lot of language revs

John Sheehan
Can't we just define a new standard whilst leaving the older style ones as aliases of the new ones?
Rich Bradshaw
Sure, they could. But the costs probably outweigh the benefits
John Sheehan
+8  A: 

Backwards compatibility and a lack of namespaces, coupled with language churn.

hazzen
+7  A: 

Because the language grew over the last 14 years from 'Personal Home Page' to one of the most popular website implementation languages. No one planned for all of these functions; they were added one at a time as necessary.

Craig Trader
A: 

This seems a little troll-y. However, it is for backwards compatibility.

PHP6 will address help address these issues as well as bringing full UTF-8 support.

Tom R
+55  A: 

The PHP language has grown somewhat organically, so the naming of functions is haphazard in parts. Many of the different formats are retained for backwards-compatibility reasons.

A slight digression, but in addition to the issues with function naming, another unfortunate side-effect of the organic growth of the language is an apparent inconsistency in argument ordering, e.g. consider the functions in_array and strstr:

bool in_array (mixed $needle, array $haystack [, bool $strict])

string strstr (string $haystack, mixed $needle [, bool $before_needle=false])

Funnily enough, PHP seems to be internally consistent with these orderings in that all string functions seem to use $haystack, $needle while array functions are the other way around, but this can take a bit of getting used to for someone new to PHP. There's a good post on ExpressionEngine talking about this particular quirk in more detail, as well as a discussion on the PHP bugs list.

As the language matures, there are various attempts to implement a more rigid and consistent naming convention - from the Zend Framework Documentation:

Function names must always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word must be capitalized. This is commonly called "camelCase" formatting.

filterInput()

getElementById()

For a slightly different take, from 20 possible reasons why PHP function names and parameters are weird:

  • PHP glues APIs and humans together, and sometimes this gets messy

  • PHP functions have been developed under many circumstances, sometimes drunk

  • PHP function naming algorithm still remains a secret and cannot be cracked

  • PHP chose to give people something fun to complain/blog/laugh about

  • PHP has other problems to solve

ConroyP
"PHP functions have been developed under many circumstances, sometimes drunk."mysql_real_escare_string can be good example. WTF!?
ciscocert
Just thought of it, but how could PHP grow *inorganically* when created by organic creatures? xD
The Wicked Flea
@ciscocert: part of the reason is that, not having namespaces at the time, the practice was to add the prefix 'mysql' to the function names.
Adriano Varoli Piazza
@Wicked Flea: that's not what 'organic' means. 'organically grown code' is one which has evolved without explicit design or planning. 'Organic food' presents much the same weirdness.
Adriano Varoli Piazza
@ciscoert "real_escape_string" comes from the MySQL C API - don't blame PHP for that one: http://dev.mysql.com/doc/refman/5.0/en/c-api-function-overview.html
Greg
A: 

PHP is an "old", fast-growing, open-source script language. The naming is just a matter of the long history... There is no "academic" company behind PHP like SUN behind Java, wich controlls the naming...

powtac
That's not completely true. Zend has been a major contributor to PHP's recent builds and since PHP 4 it has been built on the Zend Engine. In fact, Zend was started by the developers that rewrote PHP 3.
Wilco
+14  A: 

Historically many of the functions are direct maps to their C counterparts, so a lot of the naming and argument ordering weirdness in PHP is due to that.

Contributors to PHP have generally contributed the language to meet their own needs, so the language has grown organically and at times ill-diciplined. Its popularity has also meant that it has strived to maintain backwards compatability throughout its lifecycle, meaning that poor decisions about the language live on for some time after they are depreciated.

Marc Gear
+2  A: 

The answer is either Evolution or Unintelligent Design. I'm not sure which ;)

lo_fye
A: 

Initially PHP was mere a scripting language with no object oriented support (PHP 3) so, the function names used in PHP upto version 3 and 4 are mostly inspired from PERL syntax. But PHP 5 has lots of OO features like Reflection, type hinting, interface, access modifiers and list goes on. Most of these new features are inspired from JAVA. For example; implementing interface and inheriting class are same in PHP and JAVA. So, most of the new functions with their naming styles and conventions are JAVA based. One can call it evolution of PHP of a mere scripting language to a kinda robust OO language.

Prajwal Tuladhar