What are the features in PHP7

Source: Internet
Author: User
Tags deprecated throwable packt

Type declaration

Look at the code, at a glance.

Class Person{public    function age (Int. $age): string    {        return ' is '. $age;    }}

namespace and Use Keyword batch Declaration

Non-mixed mode

Use publishers\packt\{book, Ebook, Video};use function publishers\packt\{getBook, savebook};use const PUBLISHERS\PACKT \{COUNT, KEY};

Mixed mode

Use publishers\packt\{book    ,    Ebook,    Video,    function GetBook,    function SaveBook,    const COUNT,    const KEY};

Composite Mode

Use publishers\packt\{    paper\book,    electronic\ebook,    media\video};

Anonymous class

The declaration of an anonymous class and the use of the same time, with other classes have the functionality, the difference is that the anonymous class does not have a class name. The syntax is as follows:

New Class (argument) {definition};

Anonymous classes do not have a class name, but within PHP, they are assigned a globally unique name in the Reference Address table of the memory.

$name = new Class (' you ') {public    function __construct ($name)    {        echo $name;    }};

Anonymous classes can inherit methods from the parent class and the parent class.

Class packt{    protected $number;    Public function __construct ()    {        echo ' parent construct ';    }    Public Function GetNumber (): float    {        return $this->number;    }} $number = new Class (5) extends packt{public    function __construct (float $number)    {        parent::__construct ( );        $this->number = $number;    }}; echo $number->getnumber ();

An anonymous class can inherit an interface.

Interface publishers{public    function __construct (string name, string address);    Public function GetName ();    Public function getaddress ();} Class packt{    protected $number;    protected $name;    protected $address;    Public Function ...} $info = new Class (' Name ', ' address ') extends Packt implement publishers{public    function __construct (string $name, str ing $address)    {        $this->name = $name;        $this->address = $address;    }    Public Function GetName (): string    {        return $this->name;    }    Public Function GetAddress (): string    {        return $this->address;    }} echo $info->getname (). ' ' . $info->getaddress ();

Anonymous classes can be nested within a class.

Class math{public    $first _number = ten;    Public $second _number = ten;    Public function Add (): float    {        return $this->first_number + $this->second_number;    }    Public Function Mutiply_sum ()    {        return new Class () extends Math        {public            function mutiply (float $third _number): float            {                return $this->add () * $third _number;}}        ;    } $math = new Math (), Echo $math->mutiply_sum ()->mutiply (2);

Discard old-fashioned constructors

Starting with PHP4, constructors can declare themselves as constructors by naming them in a manner consistent with the class name, and in PHP7 this way the declaration constructor can still be used, but is not recommended, and will output the deprecated information Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Packt has a deprecated constructor in ... , which is recommended in PHP7 __construct() .

Throwable interface

Starting with PHP7, fatal errors in the program can be intercepted, PHP7 provides throwable interfaces, and exceptions and errors are inherited from this interface.

Error

Most fatal error cases now throw an error instance, similar to intercepting an exception, and the error instance can be intercepted by Try/catch.

try{    .} catch (Error $e) {    echo $e->getmessage ();}

Some error cases are only fruiting that are thrown, such as TypeError, Divisionbyzeroerror, ParseError, and so on.

<=> operators

<=>The operator == packs the, < and > Three comparison operators together, using the following rules.

Returns 0 if both sides of the operator are equal
Returns 1 when the left side of the operator is less than the right
Returns 1 when the left side of the operator is greater than the right

Null merge operator

??The merge operator, which can be returned directly when the first operand is present, otherwise returns the second operand.

$title = $post [' title ']?? NULL; $title = $post [' title ']?? $get [' title ']?? ' No title ';

Uniform variable Syntax

$first = [' Name ' = ' second ']; $second = ' both '; echo $ $first [' name '];echo ${sfirst[' name ']}; Php7...echo $object $methods [' title '];echo $object->{$methods [' title ']}; PHP7

Mainly because the PHP7 and the previous version of PHP parsing method is not the same, in the PHP7 with curly braces, just like the above code, otherwise it will error.

Constant array

Starting from PHP5.6 the constant array can be const declared with a keyword, and in PHP7 a constant array can be initialized by a define function.

Const STORES = [' en ', ' fr ', ' ar ']; Php5.6define (' STORES ', [' en ', ' fr ', ' ar '); Php7

Default defaults in switch

Before PHP7, the switch statement allows multiple default defaults, starting with PHP7, with only one default value, or a fatal level error.

PHP7 before switch (true) {case    ' value ':        # code ...        break;    Default:        # code ...        break;    Default:        # code ...        break;} Php7switch (True) {case    ' value ':        # code ...        break;    Default:        # code ...        break;}

Array of options in the Session_Start function

Before PHP7, you must call the Session_Start () function before using the session, and this function has no arguments to pass. All session-related configurations are in the php.ini file, starting with PHP7, which allows you to pass an array of parameter options when calling this function, which overrides the session configuration in php.ini.

Session_Start ([    ' cookie_lifetime ' = + 3600,    ' read_and_close ' = true]);

Unserialize Function Introduction Filter

Unserialize () can deserialize any type of object without any filtering, unsafe, PHP7 introduced a filter in Unserialize (), and by default allows all types of objects to be deserialized.

$result = Unserialize ($object, [' allowed_classes ' = [' book ', ' Ebook ']]);
Related Article

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.