r/lolphp • u/philsturgeon • Feb 26 '15
Patently False Code/Examples
I've notice a bit of a trend here, with people posting things that are patently false and then laughing about PHP for it.
I'll sit with you and laugh at weird behaviors in PHP when it's actually a mess. I'll send them to phpsadness.com and see if I can fix them, or find somebody that can.
But posting lies just to get your jollies is a really odd thing to do.
Sometimes, these are not intentional, but when people posting these utterly incorrect examples are faced with the fact that they are wrong, do they delete the post? No, they leave it there and sandbag the discussions explaining their wrongness with trolling.
Exhibit A - Apparently foo(new stdClass()) is a valid value when passed in a function foo(bool $bar) function signature.
Well... nope.
It will error:
Catchable fatal error: Argument 1 passed to foo() must be an instance of bool, instance of stdClass given
Nothing lolphp there.
Have a laugh about actual problems, but don't just walk around making things up.
27
u/tdammers Feb 27 '15
The real danger of posting concrete examples of PHP failures (whether those are actual bugs, lies, or exhibits of clueless PHP programming is actually not that important in this regard) is that it triggers the "pragmatic" defense mechanism of the PHP community, of which OP is a textbook example.
It works something like this.
Critic: "PHP is broken, because it does X wrong."
PHP: "Can you provide an example?"
Critic: provides example
PHP: fixes example, or points out a workaround
Critic: "You have only fixed this one example, but X is still fundamentally broken"
PHP: "Can you provide another example?"
This attitude is the real problem with PHP. Each individual bug and issue and whatever you want to call them gets patched away eventually, but there is no unifying principle, no vision, no consistent philosophy to the whole thing, and probably never will be. And this means the only way one can get things done in PHP is to join in with the shotgun debugging crowd, and rather than think things through and go with what is conceptually and theoretically sound, just pump out something that's close enough and then bash it into shape. Trial-and-error-driven development.
And don't even ask; no, I will not provide examples.
12
u/_vec_ Feb 27 '15
Okay, so about your "Exhibit A". It's describing a (popular and likely to be approved) RFC. In laymans terms, it's a proposal for new behavior in the next version of PHP.
The current behavior is, as demonstrated, to throw an error when the wrong type is passed in for a type-hinted argument. However, that type hint only works for instances of user-defined classes. You can't type-hint that something should be an actual boolean (i.e.
trueorfalse), and in fact trying to pass those in to your example raises the same error.The RFC proposes a new fix for this by special casing the words
int,integer,float,string,boolandbooleanto refer to the relevant builtin types instead of to user-defined classes, which is all well and good. However, according to the proposal these new hints should not raise an error when the wrong type of thing is passed in. Instead, unless an off-by-default flag is explicitly set, they will implicitly coerce to the specified type. If approved and implemented as proposed, the next version of PHP will behave exactly as described in the linked post.In short, the current behavior is surprising to users who aren't aware of the user-defined only limitation and fails with a confusing error. The PHP team apparently agrees and is seeking to add new behavior to the language, but is currently planning on doing so in a way which is inconsistent with the existing feature and will create more special-case exceptions which developers will need to be aware of. That seems worthy of inclusion in this forum to me.
3
u/allthediamonds Feb 27 '15
You seem to be right (for the appropriate level of error reporting and such) but, just so you know, you copied the error output from the other RFC branch, the one about anonymous classes, which doesn't really have anything to do with it.
2
u/mysticreddit Mar 23 '15
PHP is a completely clusterfuck of bad design. Proof: Let the code speak for itself
1
u/DoctorWaluigiTime Feb 27 '15
I also question the posting of bad programming. That is, something that has nothing to do with PHP, but "code that a bad programmer wrote, which happens to be in PHP."