站長資訊網
        最全最豐富的資訊網站

        聊聊有關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 '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.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 '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能起作用)

        在主體部分中指定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 '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)
        • 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視頻教程》

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 国产suv精品一区二区33| 四虎精品影院4hutv四虎| 欧美国产亚洲精品高清不卡| 午夜精品久久久久久影视777| 久久国产精品成人免费 | 四虎成人精品| 国产99精品一区二区三区免费| 2021国产精品视频| 无码国内精品人妻少妇蜜桃视频| 亚洲精品欧美二区三区中文字幕| 国产精品自产拍在线观看花钱看| 国产91精品在线| 精品国产午夜理论片不卡| 亚洲人成电影网站国产精品| 精品伦精品一区二区三区视频 | 国产观看精品一区二区三区 | 久久婷婷国产综合精品| 午夜精品久久久久久久无码| 精品久久久久久久中文字幕| 国产精品无码国模私拍视频 | 97精品伊人久久大香线蕉app| 亚洲精品乱码久久久久久自慰| 麻豆国内精品久久久久久| 国产在线观看高清精品| 国产精品自产拍在线观看花钱看| 国产精品成人观看视频网站| 91无码人妻精品一区二区三区L| 国产精品网址你懂的| 91精品在线看| 91精品国产综合久久精品| 国产办公室秘书无码精品99| 99国产精品久久| 精品一区二区三区中文字幕 | 国产成人精品天堂| 999精品在线| 国内精品91最新在线观看| 青青草原综合久久大伊人精品| 日本一区精品久久久久影院 | 中文精品99久久国产 | 亚洲日韩中文在线精品第一 | 久久精品国产精品青草|