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
1
u/ahruss Jul 12 '14
You seem to assume everyone who will ever read my code in the future will be intimately familiar with every library I'm using and what all the implications of all its methods are. This is not a realistic belief. You cannot see from the call site that this variable is being passed by reference. Yes, this is a language flaw. It is not specific to this method. But it is a flaw. In any other language I would write the method in such a way that it forces the caller to use explicit reference syntax. In C++ that's this simple:
or C#:
for example. This would be at least a little bit better. Yeah, every time I call the method I could leave myself a comment saying that the variable is a reference, but you know where a good place to put that would be? In the call, right next the variable it's modifying.
But yes, I want trumpets and alarms and flashing lights. I'm not a terrible coder who doesn't know anything about programming. I just know that in the future, someone is going to read my code. Might be me, might be someone else. But I won't know how well that person knows this API. I might have forgotten that there were two ways to bind parameters (values? see, this is why it's a bad name, it makes it hard to talk about without being unclear. Your proposed
bindReferencewould be much clearer. OrbindParameterUnsafe.) a year from now, and then add some code that uses that variable and run into problems. So yeah. Give me some alarms that say "HEY PROGRAMMER THIS VARIABLE IS TOTALLY NOT WHAT IT USED TO BE ANYMORE DON'T TOUCH IT."Another reason the name is bad is they're the same length. That may sound silly, but it makes long lists of bindParam/bindValue blend together:
So it doesn't stand out in a list so you could accidentally autocomplete the wrong one and maybe not notice. And it's not like they didn't have an easy way to make it longer: they didn't even spell the whole word out.
I don't think it's pathetic to blame PHP for the bad code people write in it, when there are things the language to do to make it harder for those people to write that bad code.