What Is New in PHP 7.1
| Category | Key Changes |
|---|---|
| New Features | Nullable types, Void return type, Iterable pseudo-type, Class constant visibility, Square bracket array destructuring |
| Improvements | Asynchronous signal handling, Catching multiple exception types, Support for keys in list() |
| Deprecations | ext/mcrypt, Several optional parameters moved to the end |
| Security | Reject invalid class names, ext/curl fixes |
What new type hints does PHP 7.1 introduce?
PHP 7.1 adds three significant type system enhancements. The iterable pseudo-type provides a way to type-hint arrays or Traversable objects, cleaning up parameter declarations.
Nullable types allow a parameter or return value to accept the specified type or null by prefixing with a question mark. This replaces the common pattern of using docblocks for this purpose.
The void return type explicitly declares that a function does not return any value. Attempting to return a value from a void function triggers a fatal error, making the code's intent clearer.
How does exception handling improve in this version?
You can now catch multiple exception types in a single block using the pipe character. This reduces code duplication when the same handling logic applies to different exception classes.
try {
// Some code that may throw
} catch (FirstException | SecondException $e) {
// Handle both exceptions
}
In practice, this is cleaner than having multiple catch blocks with the same body or creating deep inheritance hierarchies just for grouping exceptions.
What changes were made to class constants?
Class constants now support visibility modifiers (public, protected, private), bringing them in line with properties and methods. Previously, all constants were implicitly public.
class MyClass {
public const PUBLIC_CONST = 'value';
protected const PROTECTED_CONST = 'value';
private const PRIVATE_CONST = 'value';
}
This matters because it allows for better encapsulation. You can now hide implementation details by making constants private or protected.
How does list() work differently now?
The list() language construct now supports explicit key specification. You can destructure arrays with non-integer or non-sequential keys, which wasn't possible before.
$data = ['id' => 1, 'name' => 'Alice'];
list('id' => $id, 'name' => $name) = $data;
You can also use shorthand array syntax within list() for the same effect. This is particularly useful when working with associative arrays returned from database queries or APIs.
What got deprecated in PHP 7.1?
The ext/mcrypt extension is now deprecated. If you're still using it for encryption, you should migrate to OpenSSL, which is more actively maintained and secure.
Several optional parameters were moved to the end of parameter lists in various functions. This affects functions like openssl_random_pseudo_bytes() and debug_backtrace(). The old parameter order still works but generates deprecation notices.
Always check your error logs after upgrading to identify these deprecations. They won't break your application immediately but will need addressing before the next major version.
FAQ
Can I use nullable types with all type hints?
Yes, nullable types work with any type declaration except void. You prefix the type with a question mark, like ?string or ?int, to indicate it can also accept null.
What happens if I return a value from a void function?
PHP 7.1 will throw a fatal error if you try to return any value from a function declared with a void return type. This includes returning null explicitly.
Is the mcrypt extension removed in PHP 7.1?
No, it's only deprecated. The extension still functions but generates deprecation warnings. It was completely removed in PHP 7.2, so you should replace it immediately.
Can I use iterable in return type declarations?
Absolutely. The iterable type can be used for both parameter type hints and return type declarations, making your interface contracts more precise when working with arrays or traversable objects.
How do I handle the changed parameter order deprecations?
You need to update your function calls to use the new parameter order. The deprecation notice will tell you which function is affected. In most cases, you'll need to move optional parameters to the end of your function calls.