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 $name
The 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.