资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php中declare(strict_types=1)的有效范围是多少

这篇文章主要介绍“php中declare(strict_types=1)的有效范围是多少”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“php中declare(strict_types=1)的有效范围是多少”文章能帮助大家解决问题。

10多年的孝昌网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站的优势是能够根据用户设备显示端的尺寸不同,自动调整孝昌建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“孝昌网站设计”,“孝昌网站推广”以来,每个客户项目都认真落实执行。

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

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

单个文件时strict_types应写在哪里

基本语法

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

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

做了如下变更

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 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 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脚本

A.phpB.php文件require,如下

B.php脚本

执行结果

$ 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

B.php

$ 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

B.php

A.php

执行结果如下

$ 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

B.php

A.php

$ php C.php 
int(3)
  • C.php中使用strict_types=1,因此add2(1.0,2.0)以严格模式执行,但是由于没有声明变量,所以没有任何效果

  • 另一方面,具有add2()定义的B.php处于非严格模式

总结

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

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

Foo.php

bar = new Bar; // 执行严格模式
    }

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

moo = new Moo; // 执行非严格模式
    }

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

关于“php中declare(strict_types=1)的有效范围是多少”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注创新互联行业资讯频道,小编每天都会为大家更新不同的知识点。


新闻标题:php中declare(strict_types=1)的有效范围是多少
链接分享:http://cdkjz.cn/article/poipdc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220