前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP strict_types 严格模式

PHP strict_types 严格模式

作者头像
Yifans_Z
发布2023-08-23 18:49:38
3060
发布2023-08-23 18:49:38
举报
  • 严格模式的声明 必须 放在文件的顶部。

  • 严格模式不仅作用于函数参数的类型声明,也作用于函数的返回值类型。

声明 PHP 文件作为严格模式的一个好事是,实际上只适用于当前文件。这确保了这个文件是严格类型,但是他没有影响到整个项目中的其他文件。这允许你一步一步的迁移非严格模式的代码。

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

严格类型之前,int x 意味着 x must have a value coercible to an int。

  • a float (example: 13.1459 -> 13)
  • a bool (example: true -> 1)
  • a null (example: null -> 0)
  • a string with leading digits (example: “15 Trees” -> 15)

设置严格模式后,you tell the engine that int x means x must only be an int proper, no type coercion allowed。

谁给更关心 strict_type 这行?is more for the reader than for the writer. Why? Bacause it will explicitly tell the reader:

The types in this current scope are treated strictly.

对比

代码语言:javascript
复制
<?php
function add(int $a, int $b): int
{
  return $a + $b;
}

var_dump(add(1.0, 2.0));

运行输出 int(3)

代码语言:javascript
复制
<?php
declare(strict_types=1);
function add(int $a, int $b): int
{
  return $a + $b;
}

var_dump(add(1.0, 2.0));

运行输出:

代码语言:javascript
复制
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to add() must be of the type int, float given, ...

声明位置

必须在脚本最前。不能写在脚本的中间,如下写法是错误的:

代码语言:javascript
复制
<?php
function add(int $a, int $b): int
{
  return $a + $b;
}
declare(strict_types=1);

var_dump(add(1.0, 2.0));

运行后报错:

代码语言:javascript
复制
PHP Fatal error:  strict_types declaration must be the very first statement in the script in ...

不得使用 block mode 进行声明:

代码语言:javascript
复制
<?php
declare(strict_types=1) {
  var_dump(1);
}

运行后报错:

代码语言:javascript
复制
PHP Fatal error:  strict_types declaration must not use block mode in ...

多文件场景

例子 1

A.php

代码语言:javascript
复制
<?php
// 严格模式
declare(strict_types=1);
function add(int $a, int $b): int
{
  return $a + $b;
}

B.php

代码语言:javascript
复制
<?php
// 非严格模式
require 'A.php';
// 违反了 A 的定义
var_dump(add(1.0, 2.0));

运行

代码语言:javascript
复制
php B.php

int(3)

例子 2

A.php

代码语言:javascript
复制
<?php
// 非严格模式
function add(int $a, int $b): int
{
  return $a + $b;
}

B.php

代码语言:javascript
复制
<?php
// 严格模式
declare(strict_types=1);
require 'A.php';
// 违反了 A 的定义
var_dump(add(1.0, 2.0));

运行

代码语言:javascript
复制
php B.php

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to add() must be of the type int, float given, called in ...

小结

  • 函数定义时的严格模式,行为并不会出现什么不同。
  • 函数执行时的,严格模式会出现差异。
  • declare(strict_types=1); 的声明本身在 A.php 文件中完成。被 B.php 文件 require,而 B.php 并没有定义严格模式,那么执行 require 的 B.php 文件不会变成严格模式。

总结

只有在写 declare 的文件的执行部分才会执行严格模式,该文件中调用的其它函数(其它文件中的函数)也会被影响。

若果想完全使用严格模式,比较简单的方法是在所有 php 文件都写上 declare(strict_types=1);

工具

推荐自动格式化工具:symplify/easy-coding-standard

References

– EOF –

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-07-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 对比
  • 声明位置
  • 多文件场景
    • 例子 1
      • 例子 2
        • 小结
        • 总结
        • 工具
        • References
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档