Which is the new operator added in php 7?

Scalar 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 [int], floating-point numbers [float], and booleans [bool]. They augment the other types introduced in PHP 5: class names, interfaces, array and callable.

Constant arrays using define[]

Array constants can now be defined with define[]. In PHP 5.6, they could only be defined with const.

Anonymous classes

Support for anonymous classes has been added via new class. These can be used in place of full class definitions for throwaway objects:

The above example will output:

object[class@anonymous]#2 [0] {
}

Full documentation can be found in the anonymous class reference.

Unicode codepoint escape syntax

This 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.

echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";

The above example will output:

ª
ª [same as before but with optional leading 0's]
香

Closure::call[]

Closure::call[] is a more performant, shorthand way of temporarily binding an object scope to a closure and invoking it.

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 in the expectations section of the assert[] reference.

Group use declarations

Classes, functions and constants being imported from the same namespace can now be grouped together in a single use statement.

Generator Return Expressions

This feature builds upon the generator functionality introduced into PHP 5.5. It enables for a return statement to be used within a generator to enable for a final expression to be returned [return by reference is not allowed]. This value can be fetched using the new Generator::getReturn[] method, which may only be used once the generator has finished yielding values.

The above example will output:

Integer division with intdiv[]

The new intdiv[] function performs an integer division of its operands and returns it.

The above example will output:

Session options

session_start[] now accepts an array of options that override the session configuration directives normally set in php.ini.

These options have also been expanded to support session.lazy_write, which is on by default and causes PHP to only overwrite any session file if the session data has changed, and read_and_close, which is an option that can only be passed to session_start[] to indicate that the session data should be read and then the session should immediately be closed unchanged.

For example, to set session.cache_limiter to private and immediately close the session after reading it:

preg_replace_callback_array[]

The new preg_replace_callback_array[] function enables code to be written more cleanly when using the preg_replace_callback[] function. Prior to PHP 7, callbacks that needed to be executed per regular expression required the callback function to be polluted with lots of branching.

Now, callbacks can be registered to each regular expression using an associative array, where the key is a regular expression and the value is a callback.

CSPRNG Functions

Two new functions have been added to generate cryptographically secure integers and strings in a cross platform way: random_bytes[] and random_int[].

list[] can always unpack objects implementing ArrayAccess

Previously, list[] was not guaranteed to operate correctly with objects implementing ArrayAccess. This has been fixed.

Other Features

  • Class member access on cloning has been added, e.g. [clone $foo]->bar[].

PawelD

6 years ago



if < php7.0

then we will receive a syntax error, unexpected '::' [T_PAAMAYIM_NEKUDOTAYIM]

but php7 returns string[3] "baz"

I think it's not documented anywhere

Adrian Wiik

2 years ago

A good rule of thumb for remembering what the spaceship operator expression returns is to replace the spaceship operator with a minus sign [-]. If the result is negative, 0 or positive, the expression will return -1, 0 or 1 respectively.

Example:


Currying in this way is not possible in php 5.6.

Julian Sawicki

2 years ago

Currying was possible in php 5.6. But in php 7.0 it is now possible to invoke a curryied function with a one liner.

ricasiano at gmail dot com

2 years ago

Be careful on performing null coalesce on typecasted properties. Enclose them in parenthesis

Chủ Đề