New featuresScalar type declarations
Scalar
type declarations
come in two flavours: coercive (default) and strict. The following types
for parameters can now be enforced (either coercively or strictly): strings
(string), integers (
The above example will output: int(9)
To enable strict mode, a single Full documentation and examples of scalar type declarations can be found in the type declaration reference. Return type declarationsPHP 7 adds support for return type declarations. Similarly to argument type declarations, return type declarations specify the type of the value that will be returned from a function. The same types are available for return type declarations as are available for argument type declarations.
The above example will output: Array ( [0] => 6 [1] => 15 [2] => 24 ) Full documentation and examples of return type declarations can be found in the return type declarations. reference. Null coalescing operator
The null coalescing operator (
Spaceship operatorThe spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP's usual type comparison rules.
Constant arrays using define
Array constants can now be defined with
define. In PHP 5.6, they could only be defined with
Anonymous classes
Support for anonymous classes has been added via
The above example will output: object(class@anonymous)#2 (0) { } Full documentation can be found in the anonymous class reference. Unicode codepoint escape syntaxThis takes a Unicode codepoint in hexadecimal form, and outputs that codepoint in UTF-8 to a double-quoted string or a heredoc. Any valid codepoint is accepted, with leading 0's being optional.
The above example will output: ª ª (same as before but with optional leading 0's) 香 Closure::callClosure::call is a more performant, shorthand way of temporarily binding an object scope to a closure and invoking it.
The above example will output: 1 1 Filtered unserializeThis feature seeks to provide better security when unserializing objects on untrusted data. It prevents possible code injections by enabling the developer to whitelist classes that can be unserialized.
IntlCharThe new IntlChar class seeks to expose additional ICU functionality. The class itself defines a number of static methods and constants that can be used to manipulate unicode characters.
The above example will output: 10ffff COMMERCIAL AT bool(true) In order to use this class, the Intl extension must be installed. ExpectationsExpectations are a backwards compatible enhancement to the older assert function. They allow for zero-cost assertions in production code, and provide the ability to throw custom exceptions when the assertion fails. While the old API continues to be maintained for compatibility, assert is now a language construct, allowing the first parameter to be an expression rather than just a string to be evaluated or a bool value to be tested.
The above example will output: Fatal error: Uncaught CustomError: Some error message Full details on this feature, including how to configure it in both development and production environments, can be found on the manual page of the assert language construct. Group
|