類別 Status
{
const DRAFT = 'draft';
const PUBLISHED = 'published';
const ARCHIVED = 'archived';
}
函式 acceptStatus(string $status) {...}
列舉 Status
{
case Draft;
case Published;
case Archived;
}
函式 acceptStatus(Status $status) {...}
類別 BlogData
{
private Status $status;
public function __construct(Status $status)
{
$this->status = $status;
}
public function getStatus(): Status
{
return $this->status;
}
}
類別 BlogData
{
public readonly Status $status;
public function __construct(Status $status)
{
$this->status = $status;
}
}
唯讀屬性在初始化後,也就是賦值後,就不能再被更改。
它們是建模值物件和資料傳輸物件的好方法。
$foo = [$this, 'foo'];
$fn = Closure::fromCallable('strlen');
$foo = $this->foo(...);
$fn = strlen(...);
現在可以取得任何函式的參考,這稱為一級可呼叫語法。
類別 服務
{
私有 記錄器 $logger;
公開 函式 __construct(
?記錄器 $logger = null,
) {
$this->logger = $logger ?? new 空記錄器();
}
}
類別 服務
{
私有 記錄器 $logger;
公開 函式 __construct(
記錄器 $logger = new 空記錄器(),
) {
$this->logger = $logger;
}
}
物件現在可以用作預設參數值、靜態變數和全域常數,以及屬性引數。
這實際上使得使用巢狀屬性成為可能。
類別 使用者
{
/**
* @Assert\All({
* @Assert\NotNull,
* @Assert\Length(min=5)
* })
*/
公開 字串 $name = '';
}
類別 使用者
{
#[\Assert\All(
new \Assert\NotNull,
new \Assert\Length(min: 5))
]
公開 字串 $name = '';
}
函式 計數並迭代(迭代器 $value) {
if (!($value instanceof 可計數)) {
throw new 類型錯誤('值必須是可計數的');
}
foreach ($value as $val) {
echo $val;
}
計數($value);
}
函式 計數並迭代(迭代器&可計數 $value) {
foreach ($value as $val) {
echo $val;
}
計數($value);
}
當一個值需要同時滿足多個類型約束時,請使用交集類型。
目前無法將交集類型和聯集類型混合在一起,例如 A&B|C
。
函式 redirect(字串 $uri) {
header('Location: ' . $uri);
exit();
}
函式 redirectToLoginPage() {
redirect('/login');
echo 'Hello'; // <- 無效程式碼
}
函式 redirect(字串 $uri): never {
header('Location: ' . $uri);
exit();
}
函式 redirectToLoginPage(): never {
redirect('/login');
echo 'Hello'; // <- 靜態分析偵測到的無效程式碼
}
以 never
類型宣告的函式或方法表示它不會回傳值,並且會丟出例外或透過呼叫 die()
、exit()
、trigger_error()
或類似函式來結束腳本的執行。
類別 Foo
{
公開 常數 XX = "foo";
}
類別 Bar 繼承 Foo
{
公開 常數 XX = "bar"; // 沒有錯誤
}
類別 Foo
{
final 公開 常數 XX = "foo";
}
類別 Bar 繼承 Foo
{
公開 常數 XX = "bar"; // 致命錯誤
}
現在可以宣告 final 類別常數,使其無法在子類別中被覆寫。
016 === 16; // false,因為 `016` 是八進位的 `14`,容易造成混淆
016 === 14; // true
0o16 === 16; // false — 使用明確的表示法就不會混淆
0o16 === 14; // true
現在可以使用明確的 0o
前綴來撰寫八進位數字。
$httpClient->request('https://example.com/')
->then(function (Response $response) {
return $response->getBody()->buffer();
})
->then(function (string $responseBody) {
print json_decode($responseBody)['code'];
});
翻譯:
$response = $httpClient->request('https://example.com/'); 發送請求到 'https://example.com/' 並將回應儲存到 $response 變數
print json_decode($response->getBody()->buffer())['code']; 取得回應內容、緩衝、解碼 JSON 並輸出 'code' 鍵的值
翻譯:Fiber 是一種用於實現輕量級協作式並行處理的原始類型。它們是一種建立程式碼區塊的方式,這些區塊可以像 Generator 一樣暫停和恢復,但是可以從堆疊中的任何位置進行。Fiber 本身並不會神奇地提供並行處理能力,仍然需要一個事件迴圈。然而,它們允許阻塞和非阻塞實現共享相同的 API。
翻譯:Fiber 可以擺脫先前使用 `Promise::then()` 或基於 Generator 的協程所看到的樣板程式碼。函式庫通常會圍繞 Fiber 建構進一步的抽象層,因此不需要直接與它們互動。
翻譯:
$arrayA = ['a' => 1]; 定義 $arrayA 陣列
$arrayB = ['b' => 2]; 定義 $arrayB 陣列
$result = array_merge(['a' => 0], $arrayA, $arrayB); 使用 array_merge 合併陣列,後面的值會覆蓋前面的值
// ['a' => 1, 'b' => 2] 結果
翻譯:
$arrayA = ['a' => 1]; 定義 $arrayA 陣列
$arrayB = ['b' => 2]; 定義 $arrayB 陣列
$result = ['a' => 0, ...$arrayA, ...$arrayB]; 使用展開運算符 ... 合併陣列,後面的值會覆蓋前面的值
// ['a' => 1, 'b' => 2] 結果
翻譯:PHP 之前透過展開運算符支援在陣列內解包,但僅限於陣列具有整數鍵的情況。現在也可以解包具有字串鍵的陣列。
fsync
與 fdatasync
函式。array_is_list
函式。Serializable
介面已被棄用。$GLOBALS
變數限制。file_info
資源已遷移到現有的 finfo 物件。IMAP\Connection
類別物件。FTP\Connection
類別物件。GdFont
類別物件。LDAP\Connection
、LDAP\Result
和 LDAP\ResultEntry
物件。PgSql\Connection
、PgSql\Result
和 PgSql\Lob
物件。PSpell\Dictionary
、PSpell\Config
類別物件。