PHP 7.1 Class Constant Visibility

PHP 7.1 brought some changes to the way you can declare class level constants by giving the ability to mark them as public, private and protected like methods and parameters.

Visibility is important to make sure your code is used as expected when it’s extended by others, and ensure that things that shouldn’t be overwritten aren’t. Constants are important in this regard, in some cases overwriting constants is expected behavior to change default settings, in other cases constants are used internally and should never be changed when for example, you want to compare strings or set error levels.

PhpStorm 2017.2 brings this visibility change to the Extract to constant refactoring (CMD/CTRL+ALT+C). If the language level is set to PHP 7.1 (or newer) then the refactoring window will ask you the visibility of the constant you are creating.

extract-constant

– Gary & The PhpStorm Team

 

About Gary Hockin

Gary Hockin has been creating code to power web applications for 15 years, the last three of them at an extraordinary level. He is handsome, talented, funny and articulate, and also extremely modest. Gary is a valued contributor to Zend Framework 2 and is a member of the community review team. He's married and has two wonderful children, and when he's not coding, writing about code, speaking about code or reading about code, he can usually be found in the pub playing pool.
This entry was posted in Cool Feature, Newsletter. Bookmark the permalink.