BreadcrumbHomeResourcesBlog Guide To PHP 7.1 June 23, 2020 Guide to PHP 7.1 PHP DevelopmentBy Matthew Weier O’PhinneyPHP 7.1 was an incremental feature release, offering new language constructs, significant performance improvements, and lower resource utilization, making it a natural choice for fast-paced business-critical applications.In this article, we look at PHP 7.1 features, performance comparisons, end of life, and why the PHP 7.1 release marked such a big change for PHP.Table of ContentsWhat Is PHP 7.1?PHP 7.1 FeaturesPHP 7.1 PerformanceAdditional ResourcesTable of Contents1 - What Is PHP 7.1?2 - PHP 7.1 Features3 - PHP 7.1 Performance4 - Additional ResourcesBack to topWhat Is PHP 7.1?PHP 7.1 is the first feature release in the version 7 series and builds on both the performance improvements and features on PHP 7.0. In particular, it expands on variable typing features, adds features that make the language more consistent, and expands on several object-oriented and exception features.PHP 7.1 Release DatePHP 7.1 was released on December 1st, 2016.Is PHP 7.1 Still Supported?No, PHP 7.1 is no longer supported. Community support for PHP 7.1 ended on Dec 1, 2019; the last release was version 7.1.33, released on October 24, 2019. Zend by Perforce provides LTS support for 7.1 via its commercial Zend Server and ZendPHP products, and that support will continue until at least Dec 1, 2021.Back to topPHP 7.1 FeaturesNew features for PHP 7.1 fell into 3 main categories: type system additions, exception improvements, and list and array destructuring improvements. A smattering of other small features were thrown in for good measure.When it comes to type system additions, these are largely initial steps towards union types, which will be released in the upcoming PHP 8 release later this year.Nullable TypesPrior to PHP 7.1, when declaring typehints for either parameters or return values, you needed to specify exactly one type. However, many built-in PHP functions will return “null” to indicate an error condition, and many userland object methods will return “null” to indicate no work was done. As such, the language added the idea of “nullable types”. These are a form of union type, and declared by adding a “?” before the type: “?Countable” indicates a Countable implementation or a null value; “?string” indicates a string or a null value. These can be used when declaring function or method arguments, or when declaring return values.Void FunctionsThe concept of a “void” return value has long been used when documenting functions and methods to indicate that the function or method does not explicitly return a value; they simply call “return;” somewhere, or never call it at all.While PHP always evaluates such function and method return values as a “null” value, the language now allows you to declare “void” as the return value. Static code analysis tools such as phpstan and psalm, when encountering a “void” return declaration, will flag when you have inadvertently returned a value from these functions and methods.Iterable Pseudo TypeMany functions and methods can consume or return values where a valid value could be either an array or something implementing Traversable (such as a generator, or an IteratorAggregate). Since the language does not support union types (yet!), developers have had to either use a type hint that is too strict or remove type hints entirely if they wish to allow either type.PHP 7.1 introduces the “iterable” type, which is a pseudo-type acting as a union type of “array | Traversable”.Closure From CallableOccasionally, you may want to define logic within a method marked “private” that you might want to expose to the user in the form of a callable. Unfortunately, the standard callback format, “[$object, $method]”, will raise an error when consumed, as the user would be calling on functionality forbidden due to the private visibility.PHP 7.1 provides a new static method, `Closure::fromCallable()`, which allows you to wrap such callables in a Closure, automatically binding them to the instance. This then allows you to pass them around wherever they are needed.Class Constant VisibilityWhen class constants were introduced, they were assumed to have public visibility. When visibility operators were introduced, however, they did not apply to class constants, which meant it was impossible to declare constants that were private to the class.PHP 7.1 expands visibility operators to class constants. If none is provided, public visibility is assumed (as it is with other class members).Catching Multiple Exception TypesWhen catching exceptions, you will often have the same exception handling logic for multiple exceptions that do not share the same inheritance trees. As such, prior to PHP 7.1, you would need to catch each type separately.In another step towards union types, the “catch” operator now allows specifying multiple types when catching exceptions, in order to handle several types with the same code:try { } catch (FirstException | SecondException | ThirdException $e) { } Square Bracket Syntax for list()The list() operator has been around for most of PHP’s lifetime, and is used to assign values from an array:list($id, $name) = array(1, ‘Andi’);Short array syntax was introduced in PHP 5.4, allowing for more concise declaration of arrays:list($id, $name) = [1, ‘Andi’];With PHP 7.1, you can now use the same short-array syntax anywhere you would have used “list()” previously:$data = [ [1, ‘Andi’], [2, ‘Zeev’], [3, ‘Rasmus’], ]; [$id, $name] = $data[1]; // $id = 2, $name = ‘Zeev’ foreach ($data as [$id, $name]) { // do something with $id and $name } Support for Keys In ListSince arrays in PHP are used both for enumerables and maps, it would be useful for “list()” to support keys. As an example, consider the following set of data:$data = [ [‘id’ => 1, ‘name’ => ‘Andi’], [‘id’ => 2, ‘name’ => ‘Zeev’], [‘id’ => 3, ‘name’ => ‘Rasmus’], ]; In such a scenario, we cannot guarantee the order in which each of the individual “$data” entries will occur, which makes using “list()” to assign items to values error-prone.To solve this problem, PHP 7.1 introduces the ability to pull values by key name:[‘id’ => $id, ‘name’ => $name] = $data[1]; // $id = 2, $name = ‘Zeev’ foreach ($data as [‘id’ => $id, ‘name’ => $name]) { // do something with $id and $name } Back to topPHP 7.1 PerformancePHP 7.1 builds on the performance improvements first introduced in version 7.0, providing a small bump. Most CMS and eCommerce solutions will see a 1-2% improvement, while applications build closer to PHP, particularly those using frameworks, may see as much as 7% improvements.Get Long Term Support for PHP 7.1Zend helps businesses with planning, managing, and maintaining their PHP applications on end-of-life PHP versions. Take advantage of frequent bug fixes and security updates with Zend PHP consulting services.GET YOUR PHP SUPPORTEDBack to topAdditional ResourcesOfficial PHP 7.1 release notes.Kinsta PHP 7 benchmarks.Symfony PHP 7.1 benchmarks.Resource Collection - Exploring PHP VersionsBack to top
Matthew Weier O’Phinney Senior Product Manager, OpenLogic and Zend by Perforce Matthew began developing on Zend Framework (ZF) before its first public release, and led the project for Zend from 2009 through 2019. He is a founding member of the PHP Framework Interop Group (PHP-FIG), which creates and promotes standards for the PHP ecosystem — and is serving his second elected term on the PHP-FIG Core Committee.