本文给大家介绍关于关于declare(strict_types=1)的有效范围,希望对需要的朋友有所帮助!
关于declare(strict_types=1)的有效范围
declare(strict_type=1);是php7引入的严格类型检查模式的指定语法单个文件时strict_types应写在哪里基本语法<?phpfunction add(int $a, int $b): int{ return $a + $b;}var_dump(add(1.0, 2.0));
在此状态下执行独立时,输出int(3)
我们提供的是double类型,但php7能很好的处理它,和php5时代没什么区别
做了如下变更
<?phpdeclare(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:4stack 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语法不能写在脚本的中间,如下写法是错误的
<?phpfunction 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,而是编译过程中产生的错误
同样,与上述例子相似的位置,也不能使用如下语法
<?phpdeclare(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脚本<?phpdeclare(strict_types=1);function add(int $a, int $b): int{ return $a + $b;}
a.php被b.php文件require,如下
b.php脚本<?phprequire 'a.php';var_dump(add(1.0, 2.0)); //注意这里键入的是1.0和2.0浮点数,而a.php声明需要int
执行结果
$ php b.phpint(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.php被b.php引用了,就对a.php使用严格模式
a.php<?phpfunction add(int $a, int $b): int{ return $a + $b;}
b.php<?phpdeclare(strict_types=1);require 'a.php';var_dump(add(1.0, 2.0));
$ php b.phpphp 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.phpc.php<?phprequire_once 'b.php';var_dump(add(1.0, 2.0));var_dump(add2(1.0, 2.0));
b.php<?phpdeclare(strict_types=1); //在函数定义部分声明require_once 'a.php';function add2($a, $b){ return add($a, $b);}
a.php<?phpfunction 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.phpc.php<?phpdeclare(strict_types=1); //主体部分声明require_once 'b.php';var_dump(add2(1.0, 2.0));
b.php<?phprequire_once 'a.php';function add2($a, $b){ return add($a, $b);}
a.php<?phpfunction 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)的有效范围的详细内容。