PHP 7.1 introduced support for nullable types. When declaring a parameter or a return value, you can prepend its type with “?” to explicitly state that it is nullable. This way, you permit the NULL value to be passed or returned, besides the actual specified type.
PhpStorm has been supporting nullable types for some time now, and with version 2017.3, their support is enhanced even further.
To make use of it, set your project’s PHP Language Level to 7.1 or later. You can either do it manually under Settings | Languages & Frameworks | PHP or let PhpStorm take a look at your code and handle it for you automatically:
Having set the language level, you can take advantage of nullable types when performing the Extract Interface refactoring:
As well as the Change Signature refactoring:
When creating a class, you can annotate a class field with the /** @myVar null|string $myVar */ PHPDoc block thus marking it as nullable. If you generate a class constructor method at a later point, it will follow the annotation, with the nullable type hint specified:
The same applies to generating the getter and setter methods for your class fields:
With nullability support in place, you no longer need omit type declarations in your code or provide the default null value for the function parameter when you don’t want to do so.
Your JetBrains PhpStorm Team
The Drive to Develop
Subscribe to Blog updates
Thanks, we've got you!
Qodana Clone Finder: Early Access Program
In December 2020, we announced the EAP for Qodana, which is rapidly evolving into a comprehensive platform that allows companies to perform multi-level evaluations of the quality of code they own, contract, or purchase. Qodana helps you detect bugs without relying on an IDE, either on a local machi…
PhpStorm 2020.2 EAP #3
The Early Access Program for PhpStorm 2020.2 is in full swing and today we’ve got the third build of the 2020.2 EAP for you. In this blog post, you can read about improvements for Git installed in WSL2 and Search Everywhere. Download PhpStorm 2020.2 EAP (more…)…
PhpStorm 2019.3.3 Preview
We’ve just rolled out a preview for the third minor update for PhpStorm 2019.3. Please give the PhpStorm 2019.3.3 Preview build 193.6494.5 a try and share your feedback with us. (more…)…
How to Upgrade to PHP 7.4 with PhpStorm
Check out our fresh video with a few nice tips and tricks on how to update your codebase to PHP 7.4. It combines materials from the PhpStorm 2019.2 and PhpStorm 2019.3 releases, with a few cool bonuses to boot. Nikita drew our attention to the fact that setters are slower than assigning prop…