PHP7: More strict! (but only if you want it to be)
It wasn’t easy (we knew it wouldn’t be) and certainly wasn’t pretty (we sort of knew that as well), but it’s finally official: PHP7 will come with Scalar Type Hints (STH) and an optional “strict mode”.
Background, or “Let’s talk about typing, shall we?”
PHP, as you may well know, is a dynamically typed language, and it has been that way since it’s conception.
Oddly enough, you don’t see people trashing those around, do you?
I’ve never had any issues with dynamic typing. In fact, I’ve explained many times to my students that, although one could surely argue that programming with such a language could be considerably more difficult — since you may “chaotically” change types… well, pretty much anywhere — it would be very wrong, IMNSHO, to state that dynamic typing leads to bad code.
As often happens with PHP detractors criticism is either taken out of context, incomplete or outdated. This is not different when it comes to types.
For an example, PHP has, and had for a very long time, a number of ways of dealing with types, so even if you’re still able to change them, you can ensure a specific type is being used at a specific point (is_type() functions, [set|get]type(), and so on).
Even type hinting has been used for quite a while although, one might say, in an “incomplete” way: you can make functions/methods accept only arrays or instances of specific objects as parameters.
Game Changers, or “So, what’s new?”
There are basically three RFCs that were accepted for PHP’s next major, PHP7:
Scalar Type Hints v5 — (forked) RFC by Anthony Ferrara on Andrea Faulds’ original
PHP7 will come with Scalar Type Hints, namely int, float, string and bool. These will allow us to better hint on function/method parameters and return values (see the next one). Also, this won’t only apply to user-defined functions but to native functions as well.
Additionally, we’ll be able to declare a per-file strict mode, which will make PHP raise an exception (see the third RFC below) when an incoherent type is passed to functions/methods.
Return Type Declarations —RFC by Levi Morrison
This will allow us to hint on return types for functions/methods, giving us better control of what is returned and an easier way to detect incoherent behavior.
Exceptions in the engine — RFC by Nikita Popov
As of now using incorrect types — for the aforementioned array and object hints— will result in a (catchable) fatal error. As a result of this RFC being accepted, as of PHP7 this behaviour will result in an (Engine) Exception, making it easy to detect type hinting related problems and allowing us to react properly or even do a more graceful exit on these cases.
The Common Misconception or “WOW! PHP WILL BE STRICTLY TYPED!!!”
NO, IT WON’T!
This is basically a step towards a more strict way of coding in PHP. Will we see more steps in that direction in the future? We don’t know and we’re OK with that for now.
What’s brilliant about the body of work represented by these RFCs is that by implementing their concepts and specially making the “strict mode” optional the choice of being more strict remains with the programmer.
This way it provides some measurement of strictness while respecting the fact that PHP is, and — AFAWK — will always be a dynamically typed language.
Don’t wanna use it? You think this sucks? Well, carry on, don’t use it. It’s simple like that.
Wrapping up, or… Wrapping up!
PHP7 will be a fantastic new major for PHP. Along with other already decided features (see Phil Sturgeon’s post for more information), the introduction of STH and the optional strict mode represent exciting new possibilities for the language.
All of this combined will take PHP to a whole new level, keeping the language relevant and exciting, and showing everyone else that we’re not afraid of growing.
Long live PHP!
Footnotes and References
 I personally prefer “strictly” and “dynamically” rather than “strongly” and “weakly” because the latter implies that there’s something inherently wrong with dynamic typed languages.  http://en.wikipedia.org/wiki/Category:Dynamically_typed_programming_languages  In My Not So Humble Opinion  Yes, they’re so common they are a “thing” now =P  https://wiki.php.net/rfc/scalar_type_hints_v5  https://wiki.php.net/rfc/return_types  https://wiki.php.net/rfc/engine_exceptions_for_php7  https://philsturgeon.uk/php/2015/03/15/php-7-feature-freeze/