日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
聊聊有關(guān)declare(strict_types=1)的有效范圍

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

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

單個文件時strict_types應(yīng)寫在哪里


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

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

做了如下變更


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

declare語法不能寫在腳本的中間,如下寫法是錯誤的


產(chǎn)生如下錯誤

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,而是編譯過程中產(chǎn)生的錯誤

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


PHP Fatal error:  strict_types declaration must not use block mode in 
/Users/hiraku/sandbox/stricttypes/A.php on line 2

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

如下代碼

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

A.php腳本


A.phpB.php文件require,如下

B.php腳本


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

$ php B.php
int(3)

什么!!!!居然能夠執(zhí)行而不報錯!!!!!
原來是B.php并沒有聲明strict_types,所以對于B腳本來說,是默認(rèn)的松散模式

也就是說,對于strict_types有以下的行為

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

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

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);的作用

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

再增加一個require,試試3個文件嵌套

C.php


B.php


A.php


執(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錯誤

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

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

在主體部分中指定strict_types

不在B.php中途位置指定strict_types,而在主要部分即C.php指定,strict模式對所有的都有效嗎?然而,事實上strict模式只有在引用的地方有效

C.php


B.php


A.php


$ php C.php 
int(3)
  • C.php中使用strict_types=1,因此add2(1.0,2.0)以嚴(yán)格模式執(zhí)行,但是由于沒有聲明變量,所以沒有任何效果
  • 另一方面,具有add2()定義的B.php處于非嚴(yán)格模式

總結(jié)

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

也就是說,哪個文件寫了declare,哪個文件中的所有代碼就需要檢查,即使其中的代碼來自其它文件,同時,即使這個需要檢查的文件還被其它文件調(diào)用,也不改變該文件需要檢查的事實

Foo.php

bar = new Bar; // 執(zhí)行嚴(yán)格模式
    }

    public function aaa()
    {
        $this->bar->aaa(); // 執(zhí)行嚴(yán)格模式
    }
}
Bar.php

moo = new Moo; // 執(zhí)行非嚴(yán)格模式
    }

    public function aaa()
    {
        $this->moo->aaa(); // 執(zhí)行非嚴(yán)格模式
    }
}

網(wǎng)站標(biāo)題:聊聊有關(guān)declare(strict_types=1)的有效范圍
當(dāng)前路徑:http://www.5511xx.com/article/cohosdd.html