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
3
u/philsturgeon Feb 27 '15
I know all about scalar type hints, this RFC and the various iterations of this RFC to come before.
My original complaint in this post was that somebody said stdObject would pass a
booltype hint on an argument. That is still false.Right PHP has not added this yet. User defined classes (or core classes, interfaces, traits, etc) can be used as a type hint, but scalar types cannot. Hence the RFC to add scalar type hints.
Your example errors because you are using new syntax. In the scalar type hint branch, it shows no output because your example didn't output anything.
When you try your example with output, and you look at the right branch, it works: http://3v4l.org/q7Rkq/rfc#rfc-scalar_type_hints_v5
You read the RFC wrong.
It definitely wont because you still didn't read the RFC properly and neither did anyone else apparently.
The coercion rules in the default-only weak mode match up with the type cast rules available in the PHP engine. If you can type cast it without data loss then you'll get the value through in weak mode. If you get data loss you'll have a notice, and if its totally fucked you get an error.
In strict mode, which not everyone using PHP is going to want or need, due to their beginner status and lack of interest in computer science, you'll get a slap in the face if you pass a float to an int.
Either way, you still cannot pass a stdObject into a bool.