站長(zhǎng)資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        聊聊有關(guān)declare(strict_types=1)的有效范圍

        本文給大家介紹關(guān)于關(guān)于declare(strict_types=1)的有效范圍,希望對(duì)需要的朋友有所幫助!

        關(guān)于declare(strict_types=1)的有效范圍

        declare(strict_type=1);是php7引入的嚴(yán)格類(lèi)型檢查模式的指定語(yǔ)法

        單個(gè)文件時(shí)strict_types應(yīng)寫(xiě)在哪里

        基本語(yǔ)法

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

        在此狀態(tài)下執(zhí)行獨(dú)立時(shí),輸出int(3)

        我們提供的是double類(lèi)型,但php7能很好的處理它,和php5時(shí)代沒(méi)什么區(qū)別

        做了如下變更

        <?php declare(strict_types=1);    //加入這句  function add(int $a, int $b): int {     return $a + $b; }  var_dump(add(1.0, 2.0));

        TypeError產(chǎn)生,如下

        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不能寫(xiě)在腳本中間

        declare語(yǔ)法不能寫(xiě)在腳本的中間,如下寫(xiě)法是錯(cuò)誤的

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

        產(chǎn)生如下錯(cuò)誤

        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產(chǎn)生,這甚至不是Throwable,而是編譯過(guò)程中產(chǎn)生的錯(cuò)誤

        同樣,與上述例子相似的位置,也不能使用如下語(yǔ)法

        <?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

        兩個(gè)文件時(shí)strict_types如何產(chǎn)生作用

        如下代碼

        A.php腳本在開(kāi)頭聲明嚴(yán)格模式

        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浮點(diǎn)數(shù),而A.php聲明需要int

        執(zhí)行結(jié)果

        $ php B.php int(3)

        什么!!!!居然能夠執(zhí)行而不報(bào)錯(cuò)!!!!!
        原來(lái)是B.php并沒(méi)有聲明strict_types,所以對(duì)于B腳本來(lái)說(shuō),是默認(rèn)的松散模式

        也就是說(shuō),對(duì)于strict_types有以下的行為

        • 不管怎么樣,函數(shù)定義時(shí)的嚴(yán)格模式,行為并不會(huì)出現(xiàn)什么不同
        • 函數(shù)執(zhí)行時(shí)的,嚴(yán)格模式會(huì)出現(xiàn)差異
        • declare(strict_types=1);的語(yǔ)法本身在A.php文件中完成,而被B.php文件require,而B.php并沒(méi)有定義嚴(yán)格模式,那么執(zhí)行require的文件(B.php)不會(huì)變成嚴(yán)格模式

        上述解釋就如如下代碼所示,理論上A.php文件的嚴(yán)格模式已經(jīng)關(guān)閉了,然而僅僅是B.php文件設(shè)定了declare(strict_types=1);,那么即使A.php沒(méi)有設(shè)定嚴(yán)格模式,但A.phpB.php引用了,就對(duì)A.php使用嚴(yán)格模式

        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

        三個(gè)文件時(shí)declare(strict_types=1);的作用

        在函數(shù)定義部分使用declare(strict_types=1);

        再增加一個(gè)require,試試3個(gè)文件嵌套

        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);    //在函數(shù)定義部分聲明 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; }

        執(zhí)行結(jié)果如下

        $ 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)); 能正確執(zhí)行
        • var_dump(add2(1.0, 2.0));產(chǎn)生TypeError錯(cuò)誤

        也就是說(shuō),declare(strict_types=1);會(huì)按照如下方式變化

        • 定義函數(shù)本身的文件,并不能產(chǎn)生效果
        • 在定義的函數(shù)中調(diào)用其它函數(shù),嚴(yán)格模式能產(chǎn)生效果(B.php使用了strict_types=1,同時(shí)B.php調(diào)用了A.php,所以A.php能起作用)

        在主體部分中指定strict_types

        不在B.php中途位置指定strict_types,而在主要部分即C.php指定,strict模式對(duì)所有的都有效嗎?然而,事實(shí)上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)以嚴(yán)格模式執(zhí)行,但是由于沒(méi)有聲明變量,所以沒(méi)有任何效果
        • 另一方面,具有add2()定義的B.php處于非嚴(yán)格模式

        總結(jié)

        只有在寫(xiě)declare的文件的執(zhí)行部分才會(huì)執(zhí)行嚴(yán)格模式,該文件中調(diào)用的其它函數(shù)(其它文件中的函數(shù))也會(huì)被影響

        也就是說(shuō),哪個(gè)文件寫(xiě)了declare,哪個(gè)文件中的所有代碼就需要檢查,即使其中的代碼來(lái)自其它文件,同時(shí),即使這個(gè)需要檢查的文件還被其它文件調(diào)用,也不改變?cè)撐募枰獧z查的事實(shí)

        Foo.php  <?php // 這個(gè)文件的strict有效 declare(strict_types=1);  class Foo {     private $bar;      public function __construct()     {         $this->bar = new Bar; // 執(zhí)行嚴(yán)格模式     }      public function aaa()     {         $this->bar->aaa(); // 執(zhí)行嚴(yán)格模式     } }
        Bar.php  <?php // 這個(gè)文件strict無(wú)效 class Bar {     private $moo;      public function __construct()     {         $this->moo = new Moo; // 執(zhí)行非嚴(yán)格模式     }      public function aaa()     {         $this->moo->aaa(); // 執(zhí)行非嚴(yán)格模式     } }

        推薦學(xué)習(xí):《PHP視頻教程》

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 国产精品hd免费观看| 真实国产精品vr专区| 久久99精品国产麻豆蜜芽| 国产女人精品视频国产灰线| 免费观看四虎精品成人| 午夜精品视频在线观看| 国产精品多p对白交换绿帽| 一本久久精品一区二区| 久久se精品一区精品二区国产 | 国内揄拍高清国内精品对白| 四虎成人精品国产永久免费无码| 99久久综合国产精品二区| 国产亚洲精品一品区99热| 国产精品原创巨作av女教师| 午夜精品乱人伦小说区| 亚洲国产精品SSS在线观看AV| 亚洲av无码成人精品国产| 欧美日韩专区麻豆精品在线| 精品久久久久久久久久中文字幕| 国产精品内射久久久久欢欢 | 国产精品福利自产拍在线观看| 熟妇无码乱子成人精品| 亚洲精品二区国产综合野狼| 日韩午夜高清福利片在线观看欧美亚洲精品suv | 久久精品免费观看| 99精品在线播放| 国产成人精品视频一区二区不卡 | 中文字幕精品久久| 中文字幕精品一区| 亚洲精品乱码久久久久久按摩 | 国产精品99久久久久久宅男| 91精品成人免费国产片| 99久久精品费精品国产| 99R在线精品视频在线播放| mm1313亚洲国产精品无码试看| 777欧美午夜精品影院| 66精品综合久久久久久久| 国产精品狼人久久久久影院| 国产午夜精品理论片久久| 精品视频一区二区三区四区五区| 国产精品久久久久久久午夜片|