PHP 7.1 is now released – Featuring nullable types, class constants, and more.

News

December 2nd, 2016

php7.1.0.jpg

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(); // error
11}

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; // valid
11}
12
13function returns_null(): void {
14 return null; // Fatal error: A void function must not return a value
15}

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;
9
10 //Constants can only have one visibility declaration list
11 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:

Eric L. Barnes

Eric is the creator of Laravel News and has been covering Laravel since 2012.