本篇文章給大家介紹一下學習方法參數(shù)類型聲明的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。
不管從事什么行業(yè),現(xiàn)在都是活到老學到老的趨勢,特別是我們這堆碼農(nóng)。這回也不用說新技術(shù)用不上,光光是PHP文檔的學習都會發(fā)現(xiàn)非常多的知識點其實自己并沒有真正的掌握,比如說這個方法參數(shù)的類型聲明。
上次文章中,關(guān)于PHP的方法參數(shù)類型約束,我們說過方法參數(shù)的類型約束僅限于類、接口、數(shù)組或者callable回調(diào)函數(shù),其實這是不嚴謹?shù)模琍HP中也有一個嚴格模式的定義,如果指定了嚴格模式的話,普通的為方法參數(shù)類型指定普通的標量類型也是有效果的。
嚴格模式的定義:
declare (strict_types = 1);
int 類型
function testInt(int $a) { echo $a, PHP_EOL; } testInt(1); // testInt(1.1); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int // testInt('52AABB'); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int // testInt(true); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int
在嚴格模式下,很明顯地看出現(xiàn)在這個方法的參數(shù)只能接收 int 類型的值了,其他的類型都無法接收,當然也不會像之前文章說過的那樣會發(fā)生強制轉(zhuǎn)換。
float 類型
function testFloat(float $a) { echo $a, PHP_EOL; } testFloat(1); testFloat(1.1); // testFloat('52AABB'); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int // testInt(true); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int
這里需要注意的是,PHP只有 int 和 float,而且 float 是 int 的超集,所以這里是可以傳整數(shù)過來的,不過上面的 testInt(int $a) 則不能接收 1.1 這樣的 float 值。這就涉及到了上下轉(zhuǎn)換的問題,向超集轉(zhuǎn)換是OK的,但是超集向子集轉(zhuǎn)換是就不OK了。
string 類型
function testString(string $a) { echo $a, PHP_EOL; } // testString(1); // Fatal error: Uncaught TypeError: Argument 1 passed to testString() must be of the type string // testString(1.1); // Fatal error: Uncaught TypeError: Argument 1 passed to testString() must be of the type string testString('52AABB'); // testString(true); // Fatal error: Uncaught TypeError: Argument 1 passed to testString() must be of the type string
這個就不用過多解釋了,在非嚴格模式下我們?nèi)绻x string 類型的接收參數(shù)的話,其實是任何類型都可以接收過來做為 string 類型的,這里的類型轉(zhuǎn)換就不多說了,可以說在非嚴格模式下定義 string 類型的效果跟沒有任何定義是一樣的。但是嚴格模式下就不同了,真的是只能接收雙引或者單引號之內(nèi)的字符串內(nèi)容。
bool 類型
function testBool(bool $a) { var_dump($a); } testBool(true); testBool(false); // testBool('52AABB'); // Fatal error: Uncaught TypeError: Argument 1 passed to testBool() must be of the type bool // testBool(1); // Fatal error: Uncaught TypeError: Argument 1 passed to testBool() must be of the type bool
布爾值也是同理的,這里我們也只能接收 true 和 false 關(guān)鍵字的值。
新學習一個 iterable 類型
最后來介紹個新家伙,除了普通模式下的類、數(shù)組、回調(diào)函數(shù),嚴格模式下的各種標量類型聲明外,還有一個 iterable 類型的聲明,相信大家通過這個單詞也能看出來了,可迭代的類型。
function testIterable(iterable $iterator) { echo gettype($iterator), ':', PHP_EOL; foreach ($iterator as $it) { echo $it, PHP_EOL; } } testIterable([1, 2, 3]); testIterable(new ArrayIterator([1, 2, 3])); // Generator對象 testIterable((function () { yield 1; yield 2; yield 3; })()); // testIterable(1); // Fatal error: Uncaught TypeError: Argument 1 passed to testIterable() must be iterable
沒錯,它包含了數(shù)組、實現(xiàn)迭代器接口的類以及生成器相關(guān)的內(nèi)容。也就是所有可用 foreach 迭代的內(nèi)容都可以傳遞過來。生成器本身會是一個 Generator 對象,而在學習PHP生成器的使用這篇文章中,我們已經(jīng)看過這個 Generator 對象的內(nèi)容,它本身也是實現(xiàn)了 Iterator 接口。
總結(jié)
就像開頭說過的,原來在嚴格模式下我們的語法還會有這么大的差異,這回真的是長見識了。我們的學習之路還很長,也希望各位能夠持續(xù)關(guān)注一起加油?。?/p>
測試代碼:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202003/source/%E5%86%8D%E6%AC%A1%E5%AD%A6%E4%B9%A0%E6%96%B9%E6%B3%95%E5%8F%82%E6%95%B0%E7%B1%BB%E5%9E%8B%E5%A3%B0%E6%98%8E.php
推薦學習:php視頻教程