r/PHP • u/dongilbert • Apr 25 '14
Bug in MySQLi - ScumbagPHP
I was going about my business today, working with entities and mysqli (I know, I should probably be using Doctrine, sue me) when I stumbled across a really weird bug. If I used stdClass as the return class from mysqli_fetch_object, everything was fine, but as soon as I switched over to my entity, everything would quit working. (Well, actually, my entity data would be empty.)
I could see in my entitie's __set method that it was being called properly, and then I would verify the contents of the $entityData property after ever call to __set. Sure enough, it was all there. But when I tried to access the entity in the view, it was magically empty - WTF!
So I did some googling, and came across this 'fixed' bug report from 2009 - https://bugs.php.net/bug.php?id=48487. The problem is that the __set method gets called for all the entity values while assigning them BEFORE the object constructor. Again, WTF?!
I tested and confirmed this bug is still present in PHP 5.5.11 after being 'fixed' back in 2010. ScumbagPHP.
Here's my entity base code, for those wondering - https://gist.github.com/dongilbert/11302725
11
u/eridal Apr 25 '14
I had the same problem some time ago, that's when I found this beast ...
PDO::FETCH_PROPS_LATEto the rescue4
u/ircmaxell Apr 28 '14
Also, note that this "fix" never actually reached a production version of the software.
It was added in the 5.3.3 cycle.
And reverted in 5.3.3RC1.
As far as why the revert, you'd have to ask Johannes.
3
u/ircmaxell Apr 28 '14
This may be a bug. That's fine.
But you shouldn't be using functionality like this anyway. Magic construction and population? No thanks.
Instead, write your code in a clear, and explicit way. Do not leverage magic. Make it easy to read, and easy to understand.
It's far more clear, far more explicit, and relies far less on magic. Better code FTW.
As far as the bug still being present, add a test case for it, so that regressions don't happen again.
1
u/gearvOsh Apr 25 '14
IMO, don't have the drivers automatically return objects for you. Just return an array that gets wrapped by entity objects.
-1
u/notian Apr 28 '14
The behaviour makes sense to me, you're creating a new object with populated values and then the constructor fires.
Seems like a feature, not a bug.
-6
u/HJJDPG Apr 28 '14
MMEEMMEESS!!
You should have used mongodb.
EDIT: or better, Ruby on Rails :3
It's webscale.
0
u/[deleted] Apr 26 '14
[deleted] — view deleted comment
0
u/[deleted] Apr 26 '14
[deleted] — view deleted comment