PHP 7.1 new Features ____php

Source: Internet
Author: User
Tags closure curl iterable visibility

Porting from PHP 7.0.x to PHP 7.1.x

New features

http://php.net/manual/en/migration71.new-features.php


new Feature ¶ Nullable (Nullable) type ¶

The type is now allowed to be null, and when this attribute is enabled, the incoming argument or function returns the result either as a given type or as null. You can make it nullable by adding a question mark before the type. <?php
function Test (? string $name)
{
Var_dump ($name);
}

Test (' Tpunt ');
Test (NULL);
Test ();

The above routines will output:

String (5) "Tpunt"
NULL
uncaught error:too Few arguments to function test (), 0 passed in ...
Void function ¶

Based on other return value types introduced in PHP 7, a new return value type void is introduced. A method that declares a void type either omits a return statement altogether or uses an empty returns statement. For a void function, NULL is not a valid return value. <?php
Function Swap (& $left, & $right): void
{
if ($left = = = $right) {
Return
}

$tmp = $left;
$left = $right;
$right = $tmp;
}

$a = 1;
$b = 2;
Var_dump (Swap ($a, $b), $a, $b);

The above routines will output:

null
int (2)
int (1)

Trying to get the return value of a void method gets null and does not produce any warnings. The reason for this is not to affect a higher level of approach. symmetric array destructuring¶

The short array Syntax ([]) can now be used to assign the value of an array to some variables (including in foreach). This approach makes it easier to extract values from an array. <?php
$data = [
[' ID ' => 1, ' name ' => ' Tom '],
[' ID ' => 2, ' name ' => ' Fred '],
];

while ([' ID ' => $id, ' name ' => $name] = $data) {
Logic here with $id and $name
class constant Visibility ¶

The visibility of the set class constants is now supported. <?php
Class Constdemo
{
Const PUBLIC_CONST_A = 1;
Public Const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
Private Const PRIVATE_CONST = 4;
iterable Pseudo class ¶

A new pseudo class called iterable (similar to callable) is now introduced. This can be used in a parameter or return value type, which represents an object that accepts an array or implements the Traversable interface. As for subclasses, when used as arguments, subclasses can tighten the iterable type of the parent class to an array or an object that implements the traversable. For return values, subclasses can widen the array or object return value type of the parent class to iterable. <?php
Function iterator (iterable $iter)
{
foreach ($iter as $val) {
//
}
multiple exception capture processing ¶

A catch statement block can now be passed through the pipe character (|) To implement the capture of multiple exceptions. This is useful when you need to handle different exceptions from different classes at the same time. <?php
try {
Some code
catch (Firstexception | Secondexception $e) {
Handle and second exceptions
The list () now supports the key name ¶

The list () now supports specifying key names within it. This means that it can assign an array of any type to a variable (similar to a short array syntax) <?php
$data = [
[' ID ' => 1, ' name ' => ' Tom '],
[' ID ' => 2, ' name ' => ' Fred '],
];

while (list (' id ' => $id, ' name ' => $name) = $data) {
Logic here with $id and $name
support for negative string offsets ¶

All string manipulation functions that support offsets now support accepting negative numbers as offsets, including manipulating string subscripts through [] or {}. In this case, the offset of a negative number is interpreted as an offset starting at the end of the string. <?php
Var_dump ("abcdef" [-2]);
Var_dump (Strpos ("AABBCC", "B",-3));

The above routines will output:

String (1) "E"
int (3)

Negative string and array offsets are now also supported in the simple variable parsing syntax the inside of strings. <?php
$string = ' Bar ';
echo "The last character's ' $string ' is ' $string [-1] '. \ n";
?>

The above routines will output:

The last character of ' bar ' is ' r '.
Ext/openssl Support Aead¶

The Aead (modal GCM and CCM) is now supported by adding additional parameters to Openssl_encrypt () and Openssl_decrypt (). Convert callables to closure by closure::fromcallable () ¶

Closure has added a static method to quickly convert callable to a closure object. <?php
Class Test
{
Public Function exposefunction ()
{
Return closure::fromcallable ([$this, ' privatefunction ']);
}

Private Function Privatefunction ($param)
{
Var_dump ($param);
}
}

$privFunc = (new Test)->exposefunction ();
$privFunc (' some value ');

The above routines will output:

String (a) "some value"
asynchronous Signal Processing ¶

A new method named Pcntl_async_signals () is now introduced to enable asynchronous signal processing that eliminates the need for ticks (which can result in a lot of extra overhead). <?php
Pcntl_async_signals (TRUE); Turn on async signals

Pcntl_signal (Sighup, function ($sig) {
echo "Sighup\n";
});

Posix_kill (Posix_getpid (), sighup);

The above routines will output:

Sighup
HTTP/2 Server push support in ext/curl¶

Support for server push has now been added to the CURL extension (requires version 7.46 or higher). This can be adjusted by the curl_multi_setopt () function and the new constant curlmopt_pushfunction. Constants CURL_PUST_OK and

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.