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)
php
咱们提供的是double
类型,但php7
能很好的处理它,和php5
时代没什么区别php7
作了以下变动函数
<?php declare(strict_types=1); //加入这句 function add(int $a, int $b): int { return $a + $b; } var_dump(add(1.0, 2.0));
有TypeError
产生,以下ui
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
语法不能写在脚本的中间,以下写法是错误的this
<?php function add(int $a, int $b): int { return $a + $b; } declare(strict_types=1); var_dump(add(1.0, 2.0));
产生以下错误code
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
,而是编译过程当中产生的错误ip
一样,与上述例子类似的位置,也不能使用以下语法io
<?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
脚本在开头声明严格模式function
A.php脚本 <?php declare(strict_types=1); function add(int $a, int $b): int { return $a + $b; }
A.php
被B.php
文件require
,以下
B.php脚本 <?php require 'A.php'; 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.php
被B.php
引用了,就对A.php
使用严格模式
A.php <?php function add(int $a, int $b): int { return $a + $b; }
B.php <?php declare(strict_types=1); require 'A.php'; 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 'B.php'; var_dump(add(1.0, 2.0)); var_dump(add2(1.0, 2.0));
B.php <?php declare(strict_types=1); //在函数定义部分声明 require_once 'A.php'; 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
能起做用)不在B.php中途位置指定strict_types,而在主要部分即C.php指定,strict模式对全部的都有效吗?然而,事实上strict模式只有在引用的地方有效
C.php → B.php → A.php
C.php <?php declare(strict_types=1); //主体部分声明 require_once 'B.php'; var_dump(add2(1.0, 2.0));
B.php <?php require_once 'A.php'; 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)
只有在写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(); // 执行非严格模式 } }