18

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?

2
  • 2
    +1 bravo, this is one of the first things I noticed about php and have always found annoying
    – Kevin
    Commented Dec 1, 2011 at 6:56
  • Meh. Use an IDE. Commented Mar 18, 2018 at 23:51

5 Answers 5

10

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?

9
  • 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. Commented Dec 30, 2010 at 9:56
  • 4
    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. Commented Dec 30, 2010 at 10:17
  • 8
    @Shamim True, which is part of why PHP has a bad reputation in the first place ;) Commented Dec 30, 2010 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
    Commented Nov 27, 2011 at 13:34
  • 3
    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
    Commented Dec 1, 2011 at 13:35
12

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.

5
  • not everyone can add functions
    – StasM
    Commented Dec 30, 2010 at 9:51
  • @StasM: Who can, the DEV group? Any link where I can find how that group operates? Commented Dec 30, 2010 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
    Commented Dec 30, 2010 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
    Commented Dec 30, 2010 at 10:34
  • @Shamim: start with php.net and wiki.php.net.
    – StasM
    Commented Dec 30, 2010 at 18:04
5

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.

4
  • deprecating function is one thing, the easy one. Changing arguments order is more, more difficult
    – ts01
    Commented Dec 30, 2010 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. Commented Dec 30, 2010 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 :-) Commented Dec 30, 2010 at 11:49
  • Actually, what any PHP dev can do is create their own new functions to wrap these. Note also that isset() has a universal syntax for both the mentioned examples, but they just aren't deprecated as part of compiled spec. Commented Mar 30, 2016 at 19:37
5

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.

1
  • 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
    Commented Dec 1, 2011 at 13:36
2

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)

2
  • 2
    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
    Commented Dec 1, 2011 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
    Commented May 1, 2013 at 8:05

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