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

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

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: News / PHP
Laravel News Partners

Laravel Jobs

Backend Software Engineer (PHP Developer)
Austin TX, Bonita Springs FL
Certified eSupport Corp
Backend PHP Developer
Hamburg, Germany
ABOUT YOU GmbH
Full-Stack Engineer (Mid to Senior)
Remote (EST, CST)
Voxie
Senior Laravel Developer (AI Applications)
Remote from anywhere in the UK or from our London office
GreenShoot Labs
Full Stack Engineer
Remote
Shelterluv

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.