php7强类型 严格模式_PHP中的严格类型

php7强类型 严格模式

In December 2015, PHP 7 introduced scalar type declarations and with it the strict types flag.

2015年12月,PHP 7引入了标量类型声明及其严格类型标志。

To enable the strict mode, a single declare directive must be placed at the top of the file. This means that the strictness of typing for scalars is configured on a per-file basis. This directive not only affects the type declarations of parameters, but also a function’s return type.

要启用严格模式,必须在文件顶部放置一个声明指令。 这意味着按文件配置标量键入的严格性。 该指令不仅影响参数的类型声明,还影响函数的返回类型。

The good thing about declaring a PHP file as strict is that it actually applies to ONLY the current file. It ensures that this file has strict types, but it doesn’t apply to any other file in the whole project. It allows you to do, step by step, this migration from non-strict code to strict code, especially for new files or projects.

将PHP文件声明为严格的好处是,它实际上仅适用于当前文件 。 它确保该文件具有严格的类型,但不适用于整个项目中的任何其他文件。 它允许您逐步完成从非严格代码到严格代码的迁移,尤其是对于新文件或项目。

严格类型会影响强制类型 (Strict types affect coercion types)

Using hint type without strict_types may lead to subtle bugs.

使用没有strict_types提示类型可能会导致细微的错误。

Prior to strict types, int $x meant $x must have a value coercible to an int. Any value that could be coerced to an int would pass the hint type, including:

在使用严格类型之前, int $x表示$x must have a value coercible to an int 。 可以强制转换为int任何值都将通过提示类型,包括:

  • a proper int (example: 42 -> 42)

    适当的int (例如42-> 42)

  • a float (example: 13.1459 -> 13)

    float (例如:13.1459-> 13)

  • a bool (example: true -> 1)

    bool (例如: true > 1)

  • a null (example: null -> 0)

    null (例如: null -> 0)

  • a string with leading digits (example: “15 Trees” -> 15)

    带有前导数字的string (例如:“ 15棵树”-> 15)

By setting strict_types=1, you tell the engine that int $x means $x must only be an int proper, no type coercion allowed. You have the great assurance you're getting exactly and only what was given, without any conversion or potential loss.

通过设置strict_types=1 ,您告诉引擎int $x意味着$x must only be an int proper, no type coercion allowed 。 您有很大的保证,您将得到的只是给定的,而没有任何转换或潜在的损失。

谁应该关心“严格类型”这一行? (Who should care about this “strict type” line?)

Actually, declare(strict_types=1); is more for the reader than for the writer. Why? Because it will explicitly tell the reader:

实际上, declare(strict_types=1); 对读者而言,对作家而言更重要。 为什么? 因为它将明确告诉读者:

  • The types in this current scope (file/class) are treated strictly.

    当前范围 (文件/类)中的类型被严格对待。

对于读者而言, 'strict_types=1'比对于作者而言更多 ('strict_types=1' is more for the reader than for the writer)

The writer just needs to maintain such strictness while writing the expected behavior. That said, as a writer, you should care about your readers, which also includes your future self. Because you are going to be one of them.

作者只需要在编写预期行为时保持这种严格性即可。 就是说,作为作家,您应该关心您的读者,其中还包括您未来的自我。 因为你将成为其中之一。

Image for post

翻译自: https://medium.com/swlh/strict-types-in-php-d4166bd25394

php7强类型 严格模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值