Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of PHP Type Declaration property

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "PHP type declaration feature case analysis". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this "PHP type declaration feature case analysis" article can help you solve the problem.

Recently, after studying the new features of PHP7, I found that PHP is also starting to do type declaration, and the benefit of this is obvious: it can improve the speed of the program.

Let's start with this new feature:

By default, all PHP files are in weakly typed parity mode.

PHP7+ adds the feature of type declaration, which has two modes:

Mandatory mode (default mode) my understanding is to force the conversion to the required type, not to strictly distinguish the type but to make a strong turn, and the type is inconsistent and does not report an error.

Strict mode strictly verifies the type, the type is inconsistent with the declaration, and an error is reported

Scalar type declaration syntax format:

Declare (strict_types=1)

The value of strict_types:

1 indicates strict type checking mode, which acts on function calls and return statements

0 indicates weak type check mode

The type parameters that can be used are:

String

Int

Float

Bool

Interfaces

Array

Callable

Force mode (default mode)

The output result of the above program execution is: 6

Analyze: convert the string type'2' to the integer type 2, convert the float type 3 to the integer type 3, and then add it to the result 6.

Strict mode

Because the above program adopts strict mode, because the second parameter is not of type int, the error report of the execution result is as follows:

PHP Fatal error: Uncaught TypeError: Argument 2 passed to sum () must be of the type integer, string given, called in...

Returns the type declaration

PHP7+ adds support for return type declarations, which indicate the type of function return value.

The return types that can be declared are:

String

Int

Float

Bool

Interfaces

Array

Callable

I stepped on the pit.

I did cross the hole here, and in the new version of phpstorm, there will be a prompt code based on the return type to guide the addition of the return type declaration, which I added. However, later, due to business changes or code optimization, only the return value was modified (for example, array was returned before, but int was returned after modification), but forgot to modify the return type declaration (after all, I have been used to the weak language type of PHP for so many years.), this will throw an exception and cause an error. This is the pit I stepped into, and guys should pay attention to it.

Thinking

The GO language will not have the problems I mentioned above, because if the return value type and return type declaration are compiled and failed, an error will be reported at compile time.

PHP, as a weakly typed language, although it optimizes the return type declaration, it does not need us to compile and directly release and run the language before execution, so it will not be able to achieve the "compilation alarm" of the go language. PHP will directly throw an exception during execution.

The output result of the above program execution is: 5

Return an example of a type declaration error

Due to the strict mode of the above program, the return value must be int, but the calculated result is float, resulting in an error. The output of the execution is: Fatal error: Uncaught TypeError: Return value of returnIntValue () must be of the type integer, float returned....

Void function

Note: functions that define a return type of void cannot have a return value, even if it returns null.

Methods that return values of type void either omit the return statement altogether or use an empty return statement.

Example

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report