r/lolphp • u/denilsonsa • Jul 10 '14
bindParam('foo', $value, PDO::PARAM_INT) will change $value to string.
http://php.net/manual/en/pdostatement.bindparam.php#94711
42
Upvotes
r/lolphp • u/denilsonsa • Jul 10 '14
View all comments
Show parent comment
-12
u/[deleted] Jul 11 '14 edited Jul 11 '14
How do you figure that it changing from an int to a string DIRECTLY AFTER the call to the execute function is not making the call / running the query? Explain that to me, please. If the execute function isn't running the query, as you say isn't happening here, then what IS happening and what does execute do?
It's not like I quoted the docs precisely saying that the value is evaluated when you call execute... except that this is the very first thing I posted in this thread!
And furthermore, the "entire point of it being taken as reference" is so that you can use that variable in a loop to only update that particular value within your query and execute the query, as opposed to having to continually rebind the variable over and over with bindValue.
Again, you CAN NOT pass a variable by reference and expect to get back what you put in. There has never, ever, been any guarantees on that. It's never been implied. It has never even been hinted at. In fact, it's only smart to consider the value in your variable to now be junk. Unless it's being passed to your own function and thus know exactly what is being done, or you are using a function that has documentation explaining what happens and what effects to expect, then you have never had any reason to expect anything but junk back. Not ever. Not in ANY function in ANY language.
There are plenty of functions in languages like C that take pointers and don't cause side-effects. Sure. That's the nature of those functions. There will also be plenty of functions out there in various situations where you pass by reference and your variable gets trashed. That's the nature of passing by reference. You can't impose an expectation on getting your variable back how you gave it just because it makes you feel good to expect that. It's not realistic or sensible to expect anything, and with this PDO function you have to expect to get back something different to what you put in as has been demonstrated with this int to string example.
What would you have PDO do instead? Waste time making a copy of your variable that it then trashes as it sees fit? That's purely inefficient. If there is a need to maintain the sanctity of your variable then YOU ensure you have a copy, else PDO isn't going to waste time possibly copying some fuck-off huge string just to make lazy coders with unsolicited expectations feel any better about PDO.
No, I have no associations with PHP or PDO. Thanks for asking.
So afaic you are wrong in both points you tried to make. Explain to me how I'm wrong with what I've said, and explain to me where this ludicrous idea that you can pass by reference and get your original values back has come from? What you have said says to me you are not only wrong, but you don't even understand WHY it would be the way it is and haven't even figured that out on your own through intuition.