Description of the types of new features in php 7, and description of new features in php

Source: Internet
Author: User

Description of the types of new features in php 7, and description of new features in php

Preface

PHP 7 makes it possible to declare a type. PHP 7 supports the following types of parameter types:

  • Integer
  • Floating Point Type
  • String type
  • Boolean Type

The demo of function form participating in the return type declaration is as follows:

/*** @ Author yuan Chao <yccphp@163.com> */class Demo {/*** int $ name is the declaration of the parameter type *: int Is the return type declaration */public function age (int $ age): int {return $ age ;}}

We have defined a Demo class, which contains a method. When declaring a method, we specifyint $nameThe parameter to be received by the function must be of the int type. After the parameter list parentheses, we followed the following: int, which declares the data type returned by this function.

$ Demo = new Demo (); $ demo-> age (10.23); // we passed the float parameter and can also pass the check

In the above example, we passfloat Type parameter, but the code can still run normally

This is because in PHP 7, the parameter type description is not completely restricted by default, which means that what we define is just a suggestion, not a full constraint.

Of course, we can fully restrict the use of settings.

declare(strict_type=1);

At this time, when we run the above Code, we will getUncaught Type Error

This change is quite meaningful. In this way, when we are working on projects where many people are involved, there will be no problem of parameter dispassing and not knowing what this function returns.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.