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


December 2nd, 2016

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.

function answer(): ?int  {
    return null; //ok

function answer(): ?int  {
    return 42; // ok

function answer(): ?int {
    return new stdclass(); // error

Void Return Type

This requires that a function not return any value:

function should_return_nothing(): void {
    return 1; // Fatal error: A void function must not return a value

function lacks_return(): void {
    // valid

function returns_nothing(): void {
    return; // valid

function returns_null(): void {
    return null; // Fatal error: A void function must not return a value

Class constant Visibility

You now have the ability to set the visibility on class constants.

class Token {
    // Constants default to public
    const PUBLIC_CONST = 0;

        // Constants then also can have a defined visibility
        private const PRIVATE_CONST = 0;
        protected const PROTECTED_CONST = 0;
        public const PUBLIC_CONST_TWO = 0;

        //Constants can only have one visibility declaration list
        private const FOO = 1, BAR = 2;

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.