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


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

SENIOR FULL STACK SOFTWARE ENGINEER
Fort Worth, Texas
Filmsupply
PHP Web Developer
Orlando, FL
Christ for all Nations
PHP Developer
Manhattan, KS (open to Remote)
CivicPlus
Mid-Senior Laravel Developer
Exeter
Optix Solutions
Full Stack Web Developer
Las Vegas, NV
V Shred
Staff Software Engineer (Laravel)
Remote
smartbnb
Software Engineer (PHP, Laravel, Vue)
Los Angeles, CA
Relocity