Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was going through some PHP functions and I could not help notice the following:

<?php
function foo(&$var) { }

foo($a); // $a is "created" and assigned to null

$b = array();
foo($b['b']);
var_dump(array_key_exists('b', $b)); // bool(true)

$c = new StdClass;
foo($c->d);
var_dump(property_exists($c, 'd')); // bool(true)
?>

Notice the array_key_exists() and property_exists() function. In the first one, the property name(key for an array) is the first parameter while in the second one it is the second parameter. By intuition, one would expect them to have similar signature. This can lead to confusion and the development time may be wasted by making corrections of this type.

Shouldn't PHP, or any language for that matter, consider making the signatures of related functions consistent?

share|improve this question
2  
+1 bravo, this is one of the first things I noticed about php and have always found annoying –  Kevin Dec 1 '11 at 6:56
1  
PHP sucks big time. –  user61852 Jun 14 '13 at 20:47
add comment

5 Answers

up vote 6 down vote accepted

What you propose is essentially changing signatures to many existing functions. Think for a minute what effect that would have on existing code. Now suppose PHP group have released PHP version N that changes signatures of 30% of the functions. Now imagine you have to write code that runs on both PHP v.N and PHP v.{N-1} - how much fun would that be?

Now imagine you are a hoster or corporative data center manager - what incentive would you have to support PHP v.N, provided that once you switch, all the code would be broken and the users will come to your office with pitchforks and torches?

share|improve this answer
4  
+1 It's hard once you've started something the wrong way, but have such a large user-base using it in its current state. –  DesignerGuy Dec 30 '10 at 9:56
3  
That's a good point. I was actually aware of this, but my main point is they should have been aware of this in the first place. –  Shamim Hafiz Dec 30 '10 at 10:17
6  
@Shamim True, which is part of why PHP has a bad reputation in the first place ;) –  DesignerGuy Dec 30 '10 at 10:29
1  
PHP, like so many things, started as a small tool to solve a tiny problem, if one would have designed it "properly" back then it might be nicer than, but maybe wouldn't have reached the traction, so nobody would use it ... and another small tool would have "won", a tool with different inconsistencies ... –  johannes Nov 27 '11 at 13:34
2  
That's why there's such a thing as deprecation. You create new, standardized names, and deprecate the old "salad-bar" of function names but leave them around for a few releases. At some well-publicized point you come out with a new major version release that does away with them. This is how this is done. It's pure cowardice on the PHP devs' part that they haven't done this. They're riding the low barrier to entry that gives PHP an edge over other web languages, and they're successful because of that, so they don't HAVE to keep improving the core language. –  Dan Ray Dec 1 '11 at 13:35
show 4 more comments

The major source of inconsistency is that many (most?) of the php in built functions are really wrappers around some C library. The initial thinking was "I am wrapping C function xxxx therefore I should keep the parameter order the same". When it came to writing a "pure php" function this thinking was extended to "xxxx takes file and options the new function takes a file name and options so it makes sense to have yyyy take the same parameters in the same order.

The big flaw here is that the underlying C libraries were very inconsistent to begin with.

share|improve this answer
    
They also kept the C function names they were wrapping in some cases (the str functions in particular) while diverging wildly from C's naming conventions (such as they are) for other function names. –  Dan Ray Dec 1 '11 at 13:36
add comment

The (a?) reason was to remain compatible with previous versions of PHP. Instead of changing function names that would break many applications, the functions remain. However, by intuition, yes consistent function naming should be taken into consideration for new languages.

I have to disagree with you on the fact development time is wasted. Learning PHP may take longer to understand the naming of certain functions, but once mastered (or at least aware of) it becomes a non issue.

Compatibility > Consistency (at least to PHP)

share|improve this answer
1  
Other languages, I can write without constant reference to the docs. PHP I always have to worry about... is this function spelled "str_" or just "str"? Is this "array_"-something or do we not mention arrays? What does "length()" do when given a string? Oh hell, no, it's "strlen()" I really wanted... Is it "needle, haystack" or "haystack, needle"? No other language puts me through all that. –  Dan Ray Dec 1 '11 at 13:31
    
Like you I was constantly bothered by this @DanRay. I've started using NetBeans PHP IDE now though which gives me the exact info I need right in the editor. –  deed02392 May 1 '13 at 8:05
add comment

Most good languages are and strive to be consistent.

It is just the reality of the state of PHP. As StasM mentioned, it would be a nightmare to try and switch things like that after the fact. It would affect too much existing code. Often PHP simply deprecates functions and creates newer better functions that are more consistent, but that can take a lot of time.

I think successful PHP programmers either remember the particular syntax or use software that automatically tells them the syntax.

share|improve this answer
    
deprecating function is one thing, the easy one. Changing arguments order is more, more difficult –  ts01 Dec 30 '10 at 10:23
    
@ts01 nails the essential problem. With only positional parameters there's no way to know that your existing foo(a,b) should now be foo(b,a) because someone changed foo's signature. –  Frank Shearar Dec 30 '10 at 10:51
    
@ts01, @Frank: You'd have to change the name of the function, too... not a particularly good idea for stuff like "property_exists" where there is no other decent name. Personally, I'd like to see arrays become actual objects so you could say $array->key_exists('whatever') but, meh :-) –  Dean Harding Dec 30 '10 at 11:49
add comment

Because PHP is The Language Without Any Specification.

And literally everyone could add a couple of functions, and there was no question of consistency at the beginning. SO, the mess.

share|improve this answer
    
not everyone can add functions –  StasM Dec 30 '10 at 9:51
    
@StasM: Who can, the DEV group? Any link where I can find how that group operates? –  Shamim Hafiz Dec 30 '10 at 10:18
    
@StasM: ok, I exaggerated a little bit. real problem is the lack of conventions since beginning or one person responsable of code cohesion. Now is too late. I doubt if that can be changed without literally forking PHP as another language . –  ts01 Dec 30 '10 at 10:21
    
@Shamim Dev group principles of operation are two: consensus and confiance. Which is cool, but I am afraid not sufficient for good language development –  ts01 Dec 30 '10 at 10:34
    
@Shamim: start with php.net and wiki.php.net. –  StasM Dec 30 '10 at 18:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.