PHP 8.1 現已推出,它附帶了新功能與效能改良-最令人興奮的是新的 JIT 編譯器。它最近於 2021 年 11 月 25 日發布。
我將詳細示範 PHP 8.1 提供的 10 大特性,以便您可以開始在您的專案中使用它們,並改善您的 PHP 體驗。初學者和有經驗的開發人員可以從本文中受益。
#1.列舉
2.Fiber(纖維)
3.never
傳回型別
4.readonly
屬性
#5.final
類別常數
array_is_list()函數
fsync()和
fdatasync()函數
$_FILES新的用於目錄上傳的
full_path鍵
IntlDatePatternGenerator類別
enum 。它是一種逐項類型,包含固定數量的可能值。請參閱以下程式碼片段以了解如何使用枚舉。
' followed by the attribute 'name'. */ echo UserRole::WRITER->name; /** * To get the value of the enum case, you can * use the '->' followed by the attribute 'value'. */ echo UserRole::WRITER->value; ?>2. Fiber(纖維)PHP 8.1 新增了對
Fiber 的支持,這是一個低階元件,允許在 PHP 中執行並發程式碼。 Fiber 是一個程式碼區塊,它包含自己的變數和狀態堆疊。這些 Fiber 可以被視為應用程式線程,可以從主程式啟動。一旦啟動,主程式將無法掛起或終止 Fiber。它只能從 Fiber 程式碼區塊內部暫停或終止。在 Fiber 掛起後,控制權再次返回主程序,它可以從掛起的點繼續執行 Fiber。
Fiber 本身不允許同時執行多個 Fiber 或主執行緒和一個 Fiber。但是,對於 PHP 框架來說,高效管理執行堆疊並允許非同步執行是一個巨大的優勢。請參閱以下程式碼片段以了解如何使用 Fiber。
start(); /** * Fiber has been suspened from the inside. * Print some message, and then resume the Fiber. */ echo "Fiber has been suspended\n"; echo "Resuming the Fiber\n"; /** * Resume the Fiber. */ $fiber->resume(); /** * End of the example. */ echo "Fiber completed execution\n"; ?>3.
傳回類型
never的回傳類型。此
never類型可用於指示函數將在執行一組指定的任務後終止程式執行。這可以透過拋出異常、呼叫
exit()或
die()函數來完成。
請參閱以下程式碼片段以了解如何使用 never 傳回類型。never
傳回類型類似於
void傳回類型。但是,
void傳回類型在函數完成一組指定的任務後繼續執行。
* @access public * @return never */ public static function redirect($url, $httpCode = 301): never { /** * Redirect to the URL specified. */ header("Location: {$url}", true, $httpCode); die; } } Route::redirect('https://www.google.com'); ?>4.
屬性
readonly的類別屬性。已聲明為唯讀的類別屬性只能初始化一次。裡面設定的值不能改變。如果嘗試強行更新該值,應用程式將拋出錯誤。請參閱以下程式碼片段以了解如何使用唯讀屬性。
authUserID = $userID; } /** * Update Auth User ID * This function tries to update the readonly property (which is not allowed). * @method updateAuthUserID() * @param integer $userID * @author Tara Prasad Routray5.* @access public * @return void */ public function updateAuthUserID($userID) { /** * Change the value of the property as specified. * Executing this function will throw the following error; * PHP Fatal error: Uncaught Error: Cannot modify readonly property User::$authUserID */ $this->authUserID = $userID; } } /** * Initialize the class and update the value of the readonly property. */ $user = new User(30); /** * Print the readonly property value. * This will print 30. */ echo $user->authUserID; /** * Call another function inside the class and try to update the class property. */ $user->updateAuthUserID(50); /** * Print the readonly property value. */ echo $user->authUserID; ?>
類別常數
final的類別常數的支援。最終類別常數不能被修改,即使是透過繼承,這意味著它們不能被子類別擴展或覆蓋。
這個標誌不能用於私有常數,因為它不能在類別之外被存取。聲明 final 和 private 常數將導致致命錯誤。請參閱以下程式碼片段以了解如何使用最終標誌。 6. 新的
函數
array_is_list()的陣列函數。它標識指定的陣列是否具有從 0 開始的所有連續整數。如果數組是值的語意列表(一個數組,其鍵從 0 開始,都是整數,並且之間沒有間隙),則此函數傳回 true。對於空數組,它也傳回 true。請參閱以下程式碼片段以了解如何使用 array_is_list() 函數。
'apple', 1 => 2, 2 => 3] */ array_is_list(['apple', 2, 3]); /** * Returns true as the first key is zero, and keys are in sequential order. * It is same as [0 => 'apple', 1 => 'scissor'] */ array_is_list(['apple', 'orange']); /** * Returns true as the first key is zero, and keys are in sequential order. * It is same as [0 => 'apple', 1 => 'scissor'] */ array_is_list([0 => 'apple', 'orange']); /** * Returns true as the first key is zero, and keys are in sequential order. */ array_is_list([0 => 'rock', 1 => 'scissor']); ?>鍵不是從 0 開始的數組,或者鍵不是整數,或者鍵是整數但不按順序出現的數組將評估為 false。
'apple', 'orange']); /** * Returns false as the first key does not start from zero. */ array_is_list([1 => 'apple', 0 => 'orange']); /** * Returns false as all keys are not integer. */ array_is_list([0 => 'apple', 'fruit' => 'orange']); /** * Returns false as the keys are not in sequential order. */ array_is_list([0 => 'apple', 2 => 'orange']); ?>7.新的
和
fdatasync()函數
##PHP 8.1 新增了對和 请参阅以下代码片段以了解如何使用 fsync() 和 fdatasync() 函数。 PHP 8.1 添加了对字符串键数组解包的支持。为了解压数组,PHP 使用展开 PHP 8.1 添加了对 PHP 8.1 添加了对新 点赞!您已经完成了 PHP 8.1 提供的功能的学习。现在您可以继续并开始在您当前或即将进行的项目中实现上述功能。 原文:https://levelup.gitconnected.com/top-10-php-8-1-features-you-should-start-using-now-7161b91275fdfdatasync()
函數的支援。兩者都與現有fflush()
函數有相似之處,該函數目前用於將緩衝區刷新到作業系統中。然而,fsync()
和fdatasync()
刷新該緩衝區到實體儲存。它們之間的唯一差異是該fsync()
函數在同步檔案變更時包含元數據,而該fdatasync()
函數不包含元資料。 fsync()
函数将采用文件指针并尝试将更改提交到磁盘。成功时返回 true,失败时返回 false,如果资源不是文件,则会发出警告。fdatasync()
函数的工作方式相同,但速度稍快一些,因为 fsync() 将尝试完全同步文件的数据更改和有关文件的元数据(上次修改时间等),这在技术上是两次磁盘写入。8. 对字符串键数组解包的支持
(…)
运算符。PHP 7.4 中引入了这个运算符来合并两个或多个数组,但语法更简洁。但在 PHP 8.1 之前,展开运算符仅支持带数字键的数组。请参阅以下代码片段以了解如何将展开运算符用于字符串键控数组。
* string(15) "Jonathan Apples"
* [1]=>
* string(6) "Sapote"
* [2]=>
* string(6) "Pomelo"
* [3]=>
* string(9) "Jackfruit"
* [4]=>
* string(13) "Red Delicious"
* }
*/
var_dump($unpackedFruits);
?>
9.
$_FILES
新的用于目录上传的 full_path
键$_FILES
全局变量中full_path
新键的支持。在 PHP 8.1 之前,$_FILES
没有存储到服务器的相对路径或确切目录。因此,您无法使用 HTML 文件上传表单上传整个目录。新full_path
键解决了这个问题。它存储相对路径并在服务器上重建确切的目录结构,使目录上传成为可能。请参阅以下代码片段以了解如何将full_path
键与$_FILES
全局变量一起使用。 array(6) {
* ["name"]=> array(2) {
* [0]=> string(9) "image.png"
* [1]=> string(9) "image.png"
* }
* ["full_path"]=> array(2) {
* [0]=> string(25) "folder1/folder2/image.png"
* [1]=> string(25) "folder3/folder4/image.png"
* }
* ["tmp_name"]=> array(2) {
* [0]=> string(14) "/tmp/phpV1J3EM"
* [1]=> string(14) "/tmp/phpzBmAkT"
* }
* // ... + error, type, size
* }
* }
*/
var_dump($_FILES);
}
?>
10. 新的
IntlDatePatternGenerator
类IntlDatePatternGenerator
类的支持。在 PHP 8.1 之前,只能使用IntlDateFormatter
。虽然它支持昨天、今天和明天使用的八种预定义格式,但是这些格式和IntlDatePatternGenerator
不太一样。这个类允许指定日期、月份和时间的格式,并且顺序将由类自动处理。请参阅以下代码片段以了解如何使用 IntlDatePatternGenerator 类。getBestPattern($skeleton);
/**
* Use the "formatObject" function of IntlDateFormatter to print as per specified pattern.
* This will print the following:
* Date in en-US: 12/03/2021
*/
echo "Date in en-US: ". \IntlDateFormatter::formatObject($today, $enUSDatePattern, "en_US"). "\n";
/**
* =============================
* PRINTING DATE IN INDIA FORMAT
* =============================
* Initiate an instance for the IntlDatePatternGenerator class
* and provide the locale information.
* In the below example, I've used locale: en_IN.
*/
$intlDatePatternGenerator = new \IntlDatePatternGenerator("en_IN");
/**
* Get the correct date format for the locale: en_IN.
* Following function "getBestPattern" will return:
* dd/MM/YYYY
*/
$enINDatePattern = $intlDatePatternGenerator->getBestPattern($skeleton);
/**
* Use the "formatObject" function of IntlDateFormatter to print as per specified pattern.
* This will print the following:
* Date in en-IN: 03/12/2021
*/
echo "Date in en-IN: ". \IntlDateFormatter::formatObject($today, $enINDatePattern, "en_IN"). "\n";
?>