Today, the PHP team has released 7.1.0 that includes new features such as Nullable types, Void return type, Class constant visibility modifiers, and more. Here is a quick overview of some of the new features.
Nullable Types
This adds a leading question mark indicate that a type can also be null.
1function answer(): ?int { 2 return null; //ok 3} 4 5function answer(): ?int { 6 return 42; // ok 7} 8 9function answer(): ?int {10 return new stdclass(); // error11}
Void Return Type
This requires that a function not return any value:
1function should_return_nothing(): void { 2 return 1; // Fatal error: A void function must not return a value 3} 4 5function lacks_return(): void { 6 // valid 7} 8 9function returns_nothing(): void {10 return; // valid11}1213function returns_null(): void {14 return null; // Fatal error: A void function must not return a value15}
Class constant Visibility
You now have the ability to set the visibility on class constants.
1class Token { 2 // Constants default to public 3 const PUBLIC_CONST = 0; 4 5 // Constants then also can have a defined visibility 6 private const PRIVATE_CONST = 0; 7 protected const PROTECTED_CONST = 0; 8 public const PUBLIC_CONST_TWO = 0; 910 //Constants can only have one visibility declaration list11 private const FOO = 1, BAR = 2;12}
For a full list of new features take a look at the release announcement and the changelog. Also, Amo Chohan has a great article highlighting all the changes in PHP 7.1.
Filed in: