![](/img/trans.png)
[英]PHP7: Methods with a scalar type declaration refuse to type juggle NULL values, even in weak/coercive mode
[英]PHP7: shouldn't a scalar return type declaration accept integer?
我正在實現一個Iterator接口,如果我實現它返回標量(參考http://php.net/manual/en/class.iterator.php ),我得到了這個錯誤:
TypeError:Collection :: key()的返回值必須是標量的實例,返回整數
類實現:
class Collection implements \Iterator
{
public function key(): \scalar
{
return key($this->colecao);
}
// other methods implementations...
}
根據PHP參考,整數應被視為標量值( http://php.net/manual/en/migration70.new-features.php ):
標量類型聲明有兩種形式:強制(默認)和嚴格。 現在可以強制執行以下參數類型(強制或嚴格):字符串(字符串),整數(int),浮點數(浮點數)和布爾值(bool)。
在我的代碼中是否存在錯誤,您的錯誤是什么?
謝謝你的解釋!
PHP沒有稱為\\scalar
的類型。 PHP僅支持這些類型。 你的\\scalar
類型看起來像是另一個類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.