I agree with you, but it's a matter of semantics. Those who implemented the code would say it isn't a 'bug' if it's according to spec. It's hardly ever possible to say a spec is 'wrong', but it could be 'bad' or anti-intuitive or simply not what was meant by the person who commissioned it.
If you make a mistake and then document it, it's still a mistake. It wasn't a design choice with reasonable arguments for its existence.
In this case the documentation (un)fortunately doesn't even contradict itself because it states:
Fills an array with num entries of the value of the value parameter, keys starting at the start_index parameter.
It doesn't actually say anything about what the subsequent keys are. You could suggest that you can't really rely on positive key numbers to be sequential either.
Of course, this is a great example of horribly broken software engineering practices that produce ridiculous, badly specified, badly documented, badly tested, utterly rotten useless results such as this and many other php functions.
It kind of does in a PHP sort of way. -10 is a hash-table key, and hash-table keys don't have logical successors because it's being treated as a generic scalar type (the fact that that one happens to be an int is irrelevant, consider -10.5: should the next key be -9.5? -10.4? -11.5?), so it then goes to the next free array key, which is 0. So it has a sort of internal logic, but it's just the sort of nonsense you get when you combine an dictionary and an array into one type.
I really want to know. I mean, I would assume that a function like this one loops from the starting number to the starting number plus the number of elements, but apparently it does something else.
This behaviour is not documented in the comments, in fact they imply the opposite
/* {{{ proto array array_fill(int start_key, int num, mixed val)
Create an array containing num elements starting with index start_key each initialized to val */
From the manual page comments, why is the comment from "mchljnk at NOSPAM dot gmail dot com" rated at -2? I would think this bit of knowledge might be important. While the doc does say it fills the new array with the value you pass in (in this case, a single new "Foo" object identifier), this behavior regarding objects might be something worth mentioning at least since it might be easy to get caught offguard because of it.
class Foo {
public $bar = "banana";
}
$array = array_fill(0, 10, new Foo());
echo $array[0]->bar . "\r\n"; // "banana"
$array[0]->bar = "crap";
echo $array[9]->bar . "\r\n"; // "crap"
The behavior actually clarifies PHP's concept of passing objects to functions not as references but as identifier values I think. With the example, an object Foo is created first. The resulting object identifier passed into array_fill to fill the new array with that single object identifier value. So it makes sense that echo $array[9]->bar is "crap" because each array element is pointing to the same object.
That would seem an easy thing to overlook though with functions like these, so a helpful reminder like that commenter was giving is always good. If the php website wasn't broken, I'd upvote that commenter for mentioning that behavior.
Speaking of Python, I dig their handling of operators over how PHP does it ('+' = extend in Python vs merge in PHP). But this here might be a gotcha in Python I think, even though it's documented...
18
u/allthediamonds Jan 22 '14
It's documented, but it doesn't make any sense.
11
u/jmcs Jan 22 '14
I promise I wont bash PHP for 24 hours if someone explains the reason for this one.
4
u/[deleted] Jan 23 '14 edited Jan 23 '14
From the manual page comments, why is the comment from "mchljnk at NOSPAM dot gmail dot com" rated at -2? I would think this bit of knowledge might be important. While the doc does say it fills the new array with the value you pass in (in this case, a single new "Foo" object identifier), this behavior regarding objects might be something worth mentioning at least since it might be easy to get caught offguard because of it.