搜索
首页后端开发php教程聊聊有关declare(strict_types=1)的有效范围

本文给大家介绍关于关于declare(strict_types=1)的有效范围,希望对需要的朋友有所帮助!

关于declare(strict_types=1)的有效范围

declare(strict_type=1);是php7引入的严格类型检查模式的指定语法

单个文件时strict_types应写在哪里

基本语法

<?php
function add(int $a, int $b): int
{
    return $a + $b;
}

var_dump(add(1.0, 2.0));

在此状态下执行独立时,输出int(3)

我们提供的是double类型,但php7能很好的处理它,和php5时代没什么区别

做了如下变更

<?php
declare(strict_types=1);    //加入这句

function add(int $a, int $b): int
{
    return $a + $b;
}

var_dump(add(1.0, 2.0));

TypeError产生,如下

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to add() must be of 
the type integer, float given, called in /Users/hiraku/sandbox/stricttypes/A.php on line 9 and defined in 
/Users/hiraku/sandbox/stricttypes/A.php:4
Stack trace:
#0 /Users/hiraku/sandbox/stricttypes/A.php(9): add(1, 2)
#1 {main}
  thrown in /Users/hiraku/sandbox/stricttypes/A.php on line 4

strict_types不能写在脚本中间

declare语法不能写在脚本的中间,如下写法是错误的

<?php
function add(int $a, int $b): int
{
    return $a + $b;
}

declare(strict_types=1);

var_dump(add(1.0, 2.0));

产生如下错误

PHP Fatal error:  strict_types declaration must be the very first statement in the script in 
/Users/hiraku/sandbox/stricttypes/A.php on line 7

Fatal error产生,这甚至不是Throwable,而是编译过程中产生的错误

同样,与上述例子相似的位置,也不能使用如下语法

<?php
declare(strict_types=1) {
  //...
}
PHP Fatal error:  strict_types declaration must not use block mode in 
/Users/hiraku/sandbox/stricttypes/A.php on line 2

两个文件时strict_types如何产生作用

如下代码

A.php脚本在开头声明严格模式

A.php脚本

<?php
declare(strict_types=1);
function add(int $a, int $b): int
{
    return $a + $b;
}

A.phpB.php文件require,如下

B.php脚本

<?php
require &#39;A.php&#39;;
var_dump(add(1.0, 2.0));    //注意这里键入的是1.0和2.0浮点数,而A.php声明需要int

执行结果

$ php B.php
int(3)

什么!!!!居然能够执行而不报错!!!!!
原来是B.php并没有声明strict_types,所以对于B脚本来说,是默认的松散模式

也就是说,对于strict_types有以下的行为

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

上述解释就如如下代码所示,理论上A.php文件的严格模式已经关闭了,然而仅仅是B.php文件设定了declare(strict_types=1);,那么即使A.php没有设定严格模式,但A.phpB.php引用了,就对A.php使用严格模式

A.php

<?php
function add(int $a, int $b): int
{
    return $a + $b;
}
B.php

<?php
declare(strict_types=1);

require &#39;A.php&#39;;
var_dump(add(1.0, 2.0));
$ php B.php
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to add() 
must be of the type integer, float given, called in /Users/hiraku/sandbox/stricttypes/B.php on line 4 and 
defined in /Users/hiraku/sandbox/stricttypes/A.php:2

三个文件时declare(strict_types=1);的作用

在函数定义部分使用declare(strict_types=1);

再增加一个require,试试3个文件嵌套

C.php → B.php → A.php
C.php

<?php
require_once &#39;B.php&#39;;
var_dump(add(1.0, 2.0));
var_dump(add2(1.0, 2.0));
B.php

<?php
declare(strict_types=1);    //在函数定义部分声明
require_once &#39;A.php&#39;;
function add2($a, $b)
{
    return add($a, $b);
}
A.php

<?php
function add(int $a, int $b): int
{
    return $a + $b;
}

执行结果如下

$ php C.php 
int(3)
PHP Fatal error:  Uncaught TypeError: Argument 1 passed to add() must be of the type integer, float given, called in 
/Users/hiraku/sandbox/stricttypes/B.php 
on line 7 and defined in /Users/hiraku/sandbox/stricttypes/A.php:2
  • var_dump(add(1.0, 2.0)); 能正确执行
  • var_dump(add2(1.0, 2.0));产生TypeError错误

也就是说,declare(strict_types=1);会按照如下方式变化

  • 定义函数本身的文件,并不能产生效果
  • 在定义的函数中调用其它函数,严格模式能产生效果(B.php使用了strict_types=1,同时B.php调用了A.php,所以A.php能起作用)

在主体部分中指定strict_types

不在B.php中途位置指定strict_types,而在主要部分即C.php指定,strict模式对所有的都有效吗?然而,事实上strict模式只有在引用的地方有效

C.php → B.php → A.php
C.php

<?php
declare(strict_types=1);    //主体部分声明
require_once &#39;B.php&#39;;
var_dump(add2(1.0, 2.0));
B.php

<?php
require_once &#39;A.php&#39;;
function add2($a, $b)
{
    return add($a, $b);
}
A.php

<?php
function add(int $a, int $b): int
{
    return $a + $b;
}
$ php C.php 
int(3)
  • C.php中使用strict_types=1,因此add2(1.0,2.0)以严格模式执行,但是由于没有声明变量,所以没有任何效果
  • 另一方面,具有add2()定义的B.php处于非严格模式

总结

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

也就是说,哪个文件写了declare,哪个文件中的所有代码就需要检查,即使其中的代码来自其它文件,同时,即使这个需要检查的文件还被其它文件调用,也不改变该文件需要检查的事实

Foo.php

<?php
// 这个文件的strict有效
declare(strict_types=1);

class Foo
{
    private $bar;

    public function __construct()
    {
        $this->bar = new Bar; // 执行严格模式
    }

    public function aaa()
    {
        $this->bar->aaa(); // 执行严格模式
    }
}
Bar.php

<?php
// 这个文件strict无效
class Bar
{
    private $moo;

    public function __construct()
    {
        $this->moo = new Moo; // 执行非严格模式
    }

    public function aaa()
    {
        $this->moo->aaa(); // 执行非严格模式
    }
}

推荐学习:《PHP视频教程

以上是聊聊有关declare(strict_types=1)的有效范围的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:segmentfault。如有侵权,请联系admin@php.cn删除
PHP和Python:解释了不同的范例PHP和Python:解释了不同的范例Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP和Python:深入了解他们的历史PHP和Python:深入了解他们的历史Apr 18, 2025 am 12:25 AM

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

在PHP和Python之间进行选择:指南在PHP和Python之间进行选择:指南Apr 18, 2025 am 12:24 AM

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP和框架:现代化语言PHP和框架:现代化语言Apr 18, 2025 am 12:14 AM

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHP的影响:网络开发及以后PHP的影响:网络开发及以后Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?PHP类型提示如何起作用,包括标量类型,返回类型,联合类型和无效类型?Apr 17, 2025 am 12:25 AM

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。

PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?PHP如何处理对象克隆(克隆关键字)和__clone魔法方法?Apr 17, 2025 am 12:24 AM

PHP中使用clone关键字创建对象副本,并通过\_\_clone魔法方法定制克隆行为。1.使用clone关键字进行浅拷贝,克隆对象的属性但不克隆对象属性内的对象。2.通过\_\_clone方法可以深拷贝嵌套对象,避免浅拷贝问题。3.注意避免克隆中的循环引用和性能问题,优化克隆操作以提高效率。

PHP与Python:用例和应用程序PHP与Python:用例和应用程序Apr 17, 2025 am 12:23 AM

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具