What's coming in PHP 7.0
Version 7.0 of the PHP language and its interpreter are scheduled to be released December 3, following a series of eight release-candidate (RC) builds. This release will represent a major upgrade of PHP that adds several new language features and fixes some longstanding issues. In addition, the new release is said to be substantially faster, which should be a boon to those running PHP sites even if they do not choose to take advantage of changes to the language.
Operators and functions
The combined-comparison (or "spaceship") operator is a new three-way logic operator that can be used to compare two arbitrary PHP expressions and that will evaluate to one of three values (one, zero, or negative one). It takes the form <=> (much like its equivalents in Perl and Ruby). The comparison a <=> b evaluates to zero if a = b, to one if a > b, and to negative one if a < b. This new operator is, ultimately, syntactic sugar; the same tests could be expressed in somewhat more verbose form using the existing comparison operators. But it is an oft-requested feature that many developers expect due to its availability in other languages.
A second new operator is "??"—the null coalescing operator. The operator takes two operands and returns the first if it exists and it is not NULL; otherwise, the second is returned. For example:
$username = $_GET['user'] ?? 'nobody';
will set username to either the user HTTP request parameter or to 'nobody'. Here again, the operator primarily provides a convenience function to spare developers from performing a test to see if the user parameter is NULL in addition to performing the lookup.
There is also a new integer-division function, intdiv(). Many other languages have supported integer division for a long time, but it is new to PHP. The function spares developers from having to implement their own workarounds. In addition, the assert() function has been extended to support expectations. This allows the developer to optionally add a custom exception as the second argument to an assertion. The intended use case is that assertions can be placed in code during development that will trigger exceptions useful to the programmer, but that those assertions and exceptions will be removed from production code.
Data types
In PHP 7, it is possible to declare the return type of a function. This feature has been proposed on multiple occasions in the past; the proposal that finally succeeded did so by managing to not break backward compatibility: it does not introduce new types or keywords, nor does it modify any existing keywords. The syntax is relatively simple; a function declaration indicates its return type after the parameter list but before the code block begins:
function foo(): int { return 42; }
Function definitions are not required to declare their return type, except in cases where a method inherits from a parent method that does declare its return type.
PHP 7 adds type declarations for four scalar types: int, float, string, and bool. These types are available for use both as return-type declarations and as argument type hints:
function add(int $a, int $b): int { return $a + $b; }
The feature had been requested before, but an implementation was held up as the project debated whether adding it would necessitate a adding strict type checking as well. The solution adopted attempts to strike a middle ground. By default, the interpreter uses weak type checking, but developers can enable strict type checking on a per-file basis with a declaration at the start of the file:
declare(strict_types=1);
Developers can also now define array constants with the define() function:
define('ANIMALS', [ 'dog', 'cat', 'fruitbat' ]);
In earlier releases, array constants could only be defined with the const keyword, which limited them to being declared at the top-level scope. Using define() enables programmers to define array constants inside functions, loops, or other blocks.
Also new is support for anonymous classes, using the syntax:
new class (arguments) {definition}
This allows developers to create "disposable" classes that are thrown away as soon as they are no longer in use. It is akin to anonymous functions, which PHP added support for in the 5.3 release, back in 2009.
Other changes
The new release also fixes the inconsistency in how various platforms (specifically, those using different 64-bit data models) handle strings and integers. Previously, PHP used long to represent signed integers and int to handle string lengths. Unfortunately, those data types are of different sizes on Windows, SPARC64 systems, and non-SPARC64 Linux; this caused considerable trouble for developers wanting to write portable PHP code. The new release now uses data types that are guaranteed to be 64-bit on all 64-bit platforms.
Throwable exceptions are now supported, replacing many fatal errors from earlier releases.
Removing dead and deprecated code is often a welcome change, and the PHP 7 release removes functionality that was declared deprecated in the 5.x series. Furthermore, a long list of abandoned or unsupported extensions and Server Application Programming Interfaces (SAPIs) have also been removed. The SAPIs removed include connectors to AOL, Apache 1.x, Microsoft IIS, and the Tux web server.
For existing site users, perhaps the most noticeable change is that PHP 7 is reportedly up to twice as fast as PHP 5.6. This speed improvement comes from the refactoring of the core PHP interpreter that was previously known as "phpng." The 7.0 release merges in the new core, dropping the "-ng" suffix from its moniker.
Speed-ups are notoriously hard to generalize, but hosting provider Engine Yard
reports that PHP 7 is on par with the PHP just-in-time compiler used
by Facebook. The Zend web site offers one
noteworthy data point, claiming that "when PHPNG was published,
the WordPress homepage required approx. 9.4 billion CPU instructions
to execute. As of now – it requires only 2.6 billion
".
The last stable PHP release, version 5.6, debuted a bit over one
year ago, in August 2014. The 6.0 major-version number was skipped
entirely, since that branch got bogged
down in a controversial shift
to Unicode for all string handling. The new release does add a
Unicode escape character, \u, but does not incorporate the
massive changes once planned for PHP 6. Considering how divisive that
process was, it is a good sign that the 7.0 release has finally seen
the light of day, and seems to provide the functionality that many of
its fans have been asking for.
Posted Dec 3, 2015 23:03 UTC (Thu)
by flussence (guest, #85566)
[Link] (5 responses)
Posted Dec 7, 2015 15:26 UTC (Mon)
by robbe (guest, #16131)
[Link] (4 responses)
Posted Dec 8, 2015 21:00 UTC (Tue)
by flussence (guest, #85566)
[Link] (3 responses)
Posted Dec 10, 2015 11:10 UTC (Thu)
by thumperward (guest, #34368)
[Link]
Posted Dec 10, 2015 21:33 UTC (Thu)
by robbe (guest, #16131)
[Link]
Posted Feb 12, 2016 10:30 UTC (Fri)
by alexwoehr (subscriber, #100148)
[Link]
That final quote about WordPress is a bit unfortunately timed...
What's coming in PHP 7.0
What's coming in PHP 7.0
What's coming in PHP 7.0
What's coming in PHP 7.0
What's coming in PHP 7.0
What's coming in PHP 7.0