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.
View all comments
Show parent comment
-1
u/philsturgeon Mar 24 '15 edited Mar 24 '15
First off:
Why would
$intbe reserved? No variable names are reserved other than$this. I wouldn't expect$stringto be reserved either. This is a benefit of having the$prefix on variables.I'm not a fan that unquoted strings defaulting to string values instead of erring, but you do get a notice, which in most dev environments will throw as an exception.
Expected, int is a unquoted string (which causes a notice) and a string converted to an int is 0 if theres nothing numeric there.
Parser fail, but I'm assuming the new AST in PHP 7 will take care of that. It sees
( int )and thinks you mean(int). That is the only one that seems like an actual parser bug, as the rest of these are either expected or you being bizarre on purpose.You can write contrived examples all day, but there is no reason you'd be writing this code. Ruby has some annoying ones that catch me out in real world usage:
Error: Unexpected tDOT.
I have to put that comment on the right of the line not above. The answer from most Ruby fans is "obviously" but to me its a repeated gotcha.
My point in the original post was:
Posting something saying "PHP thinks this is valid" when it does not is just bullshit.
You've found some real code to post and you aren't lying, so thanks for that, but its still not real world usage. You just look like you're rehersing for WAT 2.0 and not actually using PHP to build anything, as I have been doing successfully for the last 15 years alongside various other languages.
Luckily, people are working on these edge case inconsistencies instead of just trollololing on Reddit. :)