Skip to content

If the first numeric index of an array is negative let the next one stay negative #2383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from

Conversation

pmmaga
Copy link
Contributor

@pmmaga pmmaga commented Feb 14, 2017

The current behavior of array_fill(-10, 3, true) - although documented - is unexpected.
This same behavior can also be seen in array declarations with an explicit negative index.

IMHO, this sits in a limbo between a bugfix and a language change. Or, in other words, it seems to me that the only reason why this would not be a bug would be the fact that it is documented.

Let me know what you think.

Link to the RFC: https://wiki.php.net/rfc/negative_array_index

@krakjoe
Copy link
Member

krakjoe commented Feb 15, 2017

Something that is documented, and that works the same in every released version of PHP (and hhvm) can't really be described as unexpected, or buggy. It's surprising at the most ...

I think this needs an RFC to resolve it, so I'm going to label it as such ... at the very least it requires an internals discussion.

@krakjoe krakjoe added the RFC label Feb 15, 2017
@pmmaga
Copy link
Contributor Author

pmmaga commented Feb 15, 2017

Agreed. I'll start a discussion on internals later today. Thanks for the feedback!

@andrewnester
Copy link
Contributor

sorry guys, I didn't see this PR before I sent my one, but I have alternative approach to fix this.
#2384

@sgolemon
Copy link
Member

I don't think it's a thing that needs fixing (although it is wtfaf). At most, I'd say deprecate this method and introduce a new one, but even that feels heavy handed given how positively ancient this function is.

@pmmaga
Copy link
Contributor Author

pmmaga commented Feb 16, 2017

@sgolemon, thanks for the feedback.

If you check the implementation, you'll see that this change wouldn't be limited to the array_fill function but to all arrays where the first key is negative. So, in the scope of this implementation, deprecating a function/introducing a new one wouldn't really make sense.

@andrewnester
Copy link
Contributor

@pmmaga I am wondering how it will influence existing code which relies on previous version of array indexes?

@pmmaga
Copy link
Contributor Author

pmmaga commented Feb 16, 2017

@andrewnester, yes, it would introduce a BC break in those cases.

@nikic
Copy link
Member

nikic commented Mar 8, 2017

Looks like the latest changes broke the build completely...

@pmmaga pmmaga changed the title If the first index of an array is negative let the next one stay negative [WIP] If the first index of an array is negative let the next one stay negative Mar 8, 2017
@pmmaga
Copy link
Contributor Author

pmmaga commented Mar 8, 2017

It did indeed. I didn't have the time to fix this today so I added the [WIP] to the title until I get it right again.

@pmmaga pmmaga force-pushed the negative-next-index branch from 2dc15a3 to b55489f Compare March 9, 2017 19:56
@pmmaga pmmaga changed the title [WIP] If the first index of an array is negative let the next one stay negative If the first numeric index of an array is negative let the next one stay negative Mar 9, 2017
@pmmaga
Copy link
Contributor Author

pmmaga commented Mar 9, 2017

Ok, I've managed to fix the issues and it's all green now.

@Majkl578
Copy link
Contributor

Majkl578 commented Jun 6, 2017

This definitely should not target PHP 7.2 as it breaks backward compatibility.

@ciaranmcnulty
Copy link

'Documented but unexpected' doesn't make any sense to me?

Changing documented behaviour is a clear BC break so this should target 8.0 IMO

@ciaranmcnulty
Copy link

I'd rather see an extra param to array_fill then change the default in 8.0, FWIW

@nicolas-grekas
Copy link
Contributor

nicolas-grekas commented Jun 7, 2017

see https://wiki.php.net/rfc/releaseprocess
it's pretty clear this cannot go into 7.2

@tpunt
Copy link
Contributor

tpunt commented Jun 7, 2017

I find the current behaviour of array_fill unintuitive and quite useless. But with that said, this is a completely unnecessary BC break. Given that this behaviour has been known since PHP 4.2, I don't see why it is pressing enough to need to change it ASAP in the next minor version. So I voted -1 for it targeting PHP 7.2, but I'd be +1 on targeting the next major version instead.

@pmmaga pmmaga force-pushed the negative-next-index branch from b55489f to dec8f3a Compare June 13, 2017 18:21
@pmmaga
Copy link
Contributor Author

pmmaga commented Jun 13, 2017

I have updated the RFC to target 8.0 and updated this PR to throw a E_DEPRECATED notice on the cases where the behavior will change but keep it for now.

@pmmaga pmmaga force-pushed the negative-next-index branch from dec8f3a to 34bb2da Compare June 14, 2017 17:34
@pmmaga pmmaga force-pushed the negative-next-index branch from 34bb2da to 0250615 Compare January 26, 2018 00:17
@pmmaga
Copy link
Contributor Author

pmmaga commented Mar 31, 2018

Following the vote on https://wiki.php.net/rfc/negative_array_index and given that the deprecation notice was rejected, I'm closing this PR.

@pmmaga pmmaga closed this Mar 31, 2018
@carusogabriel
Copy link
Contributor

For reference: this was implemented in PHP 8.0 as #3772.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants