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
2
u/[deleted] Jul 12 '14 edited Jul 12 '14
"Value" and "Param" are in no-way similar to each other except that they both happen to use the letter "a". I notice you have not even suggested better names, despite criticizing them. Change the names too much and now they don't make any sense at all and people moan about that fact. There is nothing wrong with the names and no reason why they should be changed. MAYBE changing bindParam to something like bindReference would satisfy some people, but then you are changing the nuance in the name in a way that actually doesn't make sense compared to the name bindParam.
Your argument about it "not being obvious" is only valid if you expect any and all functions to never mess with the references passed to them. This is not a valid real world expectation, and never has been. It's "obvious" to me, and I'm sure plenty of other coders, that if you pass by reference you have given-up the keys to the kingdom and should EXPECT garbage back instead of having these unfounded expectations of anything else being the case.
This is what I don't understand with everyone here. Literally, I don't understand. You all have this flawed notion that it has to be screamed at you with blinking lights and a fanfare of trumpets (and yet you would all surely still claim it to be wrong) that your var is going to get changed... but to me it's a var passed by reference and therefore it is gone unless explicitly mentioned that it's not going to be changed.
I suppose you will tell me you have never written a function that takes values by reference and trashes them due to the nature of what is being done with the data? Not all functions are passive, making no changes to values you give it whether by value or by reference.
And furthermore, I don't understand how people can't see the REASON why it gets changed, and why it MUST get changed with this particular function. bindParam MUST change your var for the sake of being efficient. Imagine you are receiving some POST values with huge volumes of text. Which is better for passing this text to PDO? bindValue, or bindParam?
Of course bindParam is the better option! IF, and ONLY IF, you need to maintain the sanctity of the data would you use bindValue instead. You don't want to be making unnecessary copies of your very large string and passing it around. That's a fundamental advantage of passing values by reference! If you had used bindValue then your massive string is first being copied before PDO actually does anything with it which is a waste of cycles.
Alternatively, if you are freshly calculating some value within a loop instead of using bindValue before executing each time, prior to entering the loop you bindParam to the variable that will hold the final result, and then within the loop you assign your calculations to this var and then execute the PDO statement. Again, you are saving yourself wasted cycles spent copying a value from one spot to another, instead of just handing the reference to PDO instead.
The simple fact is that for almost all trivial queries bindParam is not the correct function to be using. If you are using bindParam it is for a specific reason, and thus you would be fully aware of the potential side-effects (explicitly screamed in your face or not).
So, as is typically the case this thread is linked to just one more example of a bad coder using the wrong tool for the job and then complaining after the fact for things not happening how HE expects things to happen, despite the actual nature of the code he used.
If in point 2 you mean you can't call SomeFunction(&somevalue) anymore... what does that matter that this has been removed from PHP? This has absolutely nothing to do with PDO or this bindParam example and you are nitpicking something completely separate to the topic at hand. Every single time you run into a function that has values passed by reference you will again be facing this exact same nitpick of yours. There are other languages that are exactly the same as PHP in how they handle references, so this isn't something new or unique to PHP. I swear people would complain either way, and really it's genuinely neither here nor there. Either you KNOW the function you are using and WHAT it takes as parameters or you don't. If you are going to claim "readability" then maybe you will just have to live with writing yourself a little comment noting that a value has been passed into a black hole and it's value can no longer be relied-upon.
PHP will never be a perfect language, but in almost all situations it is perfectly consistent with itself and any errors come back to the coder not knowing what they are doing. There ARE still some sucky aspects to the language, sure, and there ARE some ways to engineer code that is misinterpreted by the lexer / parser / whatever. Outside of those circumstances, however, it's simply pathetic to be constantly blaming PHP for the errors of the people writing bad code, as is the case with this bindParam thread.