溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php參數(shù)類型如何聲明

發(fā)布時(shí)間:2022-03-18 16:32:17 來源:億速云 閱讀:224 作者:iii 欄目:大數(shù)據(jù)

這篇“php參數(shù)類型如何聲明”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“php參數(shù)類型如何聲明”文章吧。

PHP中也有一個(gè)嚴(yán)格模式的定義,如果指定了嚴(yán)格模式的話,普通的為方法參數(shù)類型指定普通的標(biāo)量類型也是有效果的。

嚴(yán)格模式的定義:

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

在嚴(yán)格模式下,很明顯地看出現(xiàn)在這個(gè)方法的參數(shù)只能接收 int 類型的值了,其他的類型都無法接收,當(dāng)然也不會(huì)像之前文章說過的那樣會(huì)發(fā)生強(qiáng)制轉(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
 

這個(gè)就不用過多解釋了,在非嚴(yán)格模式下我們?nèi)绻x string 類型的接收參數(shù)的話,其實(shí)是任何類型都可以接收過來做為 string 類型的,這里的類型轉(zhuǎn)換就不多說了,可以說在非嚴(yán)格模式下定義 string 類型的效果跟沒有任何定義是一樣的。但是嚴(yán)格模式下就不同了,真的是只能接收雙引或者單引號(hào)之內(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)鍵字的值。

新學(xué)習(xí)一個(gè) iterable 類型

最后來介紹個(gè)新家伙,除了普通模式下的類、數(shù)組、回調(diào)函數(shù),嚴(yán)格模式下的各種標(biāo)量類型聲明外,還有一個(gè) iterable 類型的聲明,相信大家通過這個(gè)單詞也能看出來了,可迭代的類型。

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

沒錯(cuò),它包含了數(shù)組、實(shí)現(xiàn)迭代器接口的類以及生成器相關(guān)的內(nèi)容。也就是所有可用 foreach 迭代的內(nèi)容都可以傳遞過來。生成器本身會(huì)是一個(gè) Generator 對象。

以上就是關(guān)于“php參數(shù)類型如何聲明”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI