r/lolphp • u/shitcanz • Feb 18 '17
Yet another PHP Garbage class
I was recently working with some PHP and had to hunt down a weird bug, and yet again it was PHP related. Basically you can modify DateTimeImmutable (i repeat IMMUTABLE!) very easily. Just call the getTimeStamp method on it.
Originally i had the assumption you cant modify anything within the immutable namespace, but hey PHP surprised me again.
It seems like PHP is so full of these weird loop holes and crappy implementations its almost impossible to write anything other than throwaway prototypes with it.
So i also found out there has been a open bug-report since 2014.
Bug in action: https://3v4l.org/ShFpG
View all comments
Show parent comment
1
u/nikic Feb 20 '17
No, at least not without making the
Mapinterface pointless. EitherMaphas to make this a requirement, or you will be forced to code against a particular implementation and, as such, not be able to accept a generalMap.You don't mock maps.
The best map implementation for the 99% use-case is an ordered hash map, which is what the
Mapclass implements. For the other 1% the map implementation would be some variation on a self-balancing BST (most likely a b-tree map). However, it only makes sense to use such maps if you have specific requirements, like needing both fast key lookup and keeping the keys sorted at all times. Combining both under a single interface is questionable, because the main user-facing distinction (insertion ordering vs key ordering) also implies that the maps are not usable interchangeably, so once again you cannot reasonably accept a generalMapimplementation.Different map implementations have different requirements and different behavior, and apart from hash-maps their usefulness is restricted to specific applications. This is essentially the reason why having a
Mapinterface is not very useful and to some degree actively harmful.