Strict typing in PHP 7 - poll results
I maintain a number of PHP packages, and with the release of PHP 7, I’ve been starting to wonder when a good time is to switch.
Yes, there’s the speed thing too, and that’s a great reason to want to update to PHP 7, but it doesn’t force you to change the minimum required version.
The improved typing system will make all my sources a lot more robust, and and I expect to find a lot of hidden bugs by just implementing them.
But PHP wouldn’t be PHP if there wasn’t something odd about this feature. Type hinting comes in two flavors: strict and non-strict. This is the result of a long battle between two camps, a strict and non-strict camp, which in the end was resolved by this compromise.
Now by default PHP acts in non-strict mode, and if you’d like to opt-in to strict-mode, you’ll need to start every PHP file with this statement:
<?php declare(strict_types=1); // Rest of your code here ?>
declarestatement here doesn’t have to be on the same line as
<?php, but I have a strong suspicion that this will be the leading declaration style.
So I was curious about everyone and whether you will be using strict mode or not. Results are in:
Will you be putting declare(strict_types=1); on top of your PHP 7 code by default?— Evert Pot (@evertp) January 13, 2016
This definitely suggests that most people will want strict mode (and that perhaps it should have been the default). It also tells me that a lot of people are unfamiliar with the feature. If that’s you, I can recommend eeading the manual, but if anyone has a good blog resource that explains things in more detail, let me know!
The nice thing about how the different modes are implemented though, is that you don’t have to worry if one library uses strict mode and another is not.
Setting your source to strict mode influences only the caller. So ultimately
strict_mode only really has an effect on whoever wants to use it, and
their own sources.
Some people are still clearly unhappy with this though.
Justin Martin is working on a PHP stream wrapper to automatically add
declare upon including PHP files:
Joe Watkins is solving the same problem, but via a PHP extension:
Yes! We started adding that to the files in our codebase on the day we deployed PHP 7 to our servers and it already uncovered a lot of bugs and inconsistencies, most often passing different parameters than was intended to invoked functions and methods.
Drew McLellan is not so lucky:
@evertp I write code with a minimum requirement of PHP 5.3. Being able to require PHP 7 is many many years away.— Drew McLellan (@drewm) January 14, 2016