您現在應該開始使用的 10 個 PHP 8.1 功能

oidns 發表於 2021-12-06
PHP

您現在應該開始使用的 10 個 PHP 8.1 功能

PHP 8.1 現已推出,它附帶了新功能和效能改進——最令人興奮的是新的 JIT 編譯器。它最近於 2021 年 11 月 25 日釋出。

我將詳細演示 PHP 8.1 提供的 10 大特性,以便您可以開始在您的專案中使用它們,並改善您的 PHP 體驗。初學者和有經驗的開發人員可以從本文中受益。

  1. 列舉
  2. 纖維
  3. “從不”返回型別
  4. “只讀”屬性
  5. 最終類常量
  6. 新的“array_is_list()”函式
  7. 新的“fsync()”和“fdatasync()”函式
  8. 對字串鍵陣列解包支援
  9. 用於目錄上傳的“$_FILES”中新的“full_path”鍵
  10. 新的“IntlDatePatternGenerator”類

PHP 8.1 新增了對列舉的支援,常被簡寫為 enum 。它是一種逐項型別,包含固定數量的可能值。請參閱以下程式碼片段以瞭解如何使用列舉。

<?php

/**
 * Declare an enumeration.
 * It can also contain an optional 'string' or 'int' value. This is called backed Enum.
 * Backed enums (if used) should match the following criteria:
 * - Declare the scalar type, whether string or int, in the Enum declaration.
 * - All cases have values.
 * - All cases contain the same scalar type, whether string or int.
 * - Each case has a unique value.
 */
enum UserRole: string {
    case ADMIN    = '1';
    case GUEST    = '2';
    case WRITER   = '3';
    case EDITOR   = '4';
}

/**
 * You can access a case by using
 * the '::' scope resolution operator.
 * And, to get the name of the enum case, you
 * can use the '->' 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;

?>

PHP 8.1 新增了對 Fibers 的支援,這是一個低階元件,允許在 PHP 中執行併發程式碼。Fiber 是一個程式碼塊,它包含自己的變數和狀態堆疊。這些 Fiber 可以被視為應用程式執行緒,可以從主程式啟動。一旦啟動,主程式將無法掛起或終止 Fiber。它只能從 Fiber 程式碼塊內部暫停或終止。在 Fiber 掛起後,控制權再次返回到主程式,它可以從掛起的點繼續執行 Fiber。

Fiber 本身不允許同時執行多個 Fiber 或主執行緒和一個 Fiber。但是,對於 PHP 框架來說,高效管理執行堆疊並允許非同步執行是一個巨大的優勢。

請參閱以下程式碼片段以瞭解如何使用 Fibers。

<?php

/**
 * Initialize the Fiber.
 */
$fiber = new Fiber(function(): void {
    /**
     * Print some message from inside the Fiber.
     * Before the Fiber gets suspended.
     */
    echo "Welcome to Fiber!\n";
    /**
     * Suspend the Fiber.
     */
    Fiber::suspend();
    /**
     * Print some message from inside the Fiber.
     * After the Fiber gets resumed.
     */
    echo "Welcome back to Fiber!\n";
});

/**
 * Print a message before starting a Fiber.
 */
echo "Starting a Fiber\n";
/**
 * Start the Fiber.
 */
$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";

?>

PHP 8.1 新增了名為never的返回型別。該never型別可用於指示函式將在執行一組指定的任務後終止程式執行。這可以通過丟擲異常、呼叫exit()die()函式來完成。

從不返回型別類似於void返回型別。但是,void返回型別在函式完成一組指定的任務後繼續執行。

請參閱以下程式碼片段以瞭解如何使用從不返回型別。


<?php

/**
 * Route Class
 */
class Route {

    /**
     * Constructor of the class
     * @return void
     */
    public function __construct() {

    }

    /**
     * Redirect To a Page
     * This function redirects to an URL specified by the user.
     * @method redirect()
     * @param string $url
     * @param integer $httpCode
     * @author Tara Prasad Routray <[email protected]>
     * @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');

?>

PHP 8.1 新增了名為readonly的類屬性。已宣告為只讀的類屬性只能初始化一次。裡面設定的值不能改變。如果嘗試強行更新該值,應用程式將丟擲錯誤。請參閱以下程式碼片段以瞭解如何使用只讀屬性。

<?php

/**
 * User Class
 */
class User {
    /**
     * Declare a variable with readonly property.
     * @var $authUserID
     * @access public
     */
    public readonly int $authUserID;
    /**
     * Constructor of the class.
     * @param integer $userID
     * @return void
     */
    public function __construct($userID) {
        /**
         * Change the value of the property as specified.
         * Updating the value of readonly properties are
         * allowed only through the constructor.
         */
        $this->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 Routray <[email protected]>
     * @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;

?>

PHP 8.1 新增了對名為final的類常量的支援。最終類常量不能被修改,即使是通過繼承,這意味著它們不能被子類擴充套件或覆蓋。

這個標誌不能用於私有常量,因為它不能在類之外被訪問。宣告 final 和 private 常量將導致致命錯誤。

請參閱以下程式碼片段以瞭解如何使用最終標誌。

<?php

/**
 * UserRole Class
 */
class UserRole {
    /**
     * Declare a final class constant with a value.
     */
    final public const ADMIN = '1';
}

/**
 * User Class extending the UserRole Class
 */
class User extends UserRole {
    /**
     * Declare another constant with the same name
     * as of the parent class to override the value.
     * 
     * Note: Overriding the value will throw the following error:
     * PHP Fatal error:  User::ADMIN cannot override final constant UserRole::ADMIN
     */
    public const ADMIN = '2';
}

?>

PHP 8.1 新增了名為array_is_list()的陣列函式。它標識指定的陣列是否具有從 0 開始的所有連續整數。如果陣列是值的語義列表(一個陣列,其鍵從 0 開始,都是整數,並且之間沒有間隙),則此函式返回 true。對於空陣列,它也返回 true。請參閱以下程式碼片段以瞭解如何使用 array_is_list() 函式。

<?php

/**
 * Returns true for empty array.
 */
array_is_list([]);
/**
 * Returns true for sequential set of keys.
 */
array_is_list([1, 2, 3]);
/**
 * Returns true as the first key is zero, and keys are in sequential order.
 * It is same as [0 => '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']);

?>

鍵不是從零開始的陣列,或者鍵不是整數,或者鍵是整數但不按順序出現的陣列將評估為 false。

<?php

/**
 * Returns false as the first key does not start from zero.
 */
array_is_list([1 => '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']); 

?>

PHP 8.1 新增了對fsync()fdatasync()函式的支援。兩者都與現有fflush()函式有相似之處,該函式當前用於將緩衝區重新整理到作業系統中。然而,fsync()fdatasync()重新整理該緩衝區到物理儲存。它們之間的唯一區別是該fsync()函式在同步檔案更改時包含後設資料,而該fdatasync()函式不包含後設資料。

fsync()函式將採用檔案指標並嘗試將更改提交到磁碟。成功時返回 true,失敗時返回 false,如果資源不是檔案,則會發出警告。該fdatasync()函式的工作方式相同,但速度稍快一些,因為 fsync() 將嘗試完全同步檔案的資料更改和有關檔案的後設資料(上次修改時間等),這在技術上是兩次磁碟寫入。

請參閱以下程式碼片段以瞭解如何使用 fsync() 和 fdatasync() 函式。

<?php

/**
 * Declare a variable and assign a filename.
 */
$fileName = 'notes.txt';
/**
 * Create the file with read and write permission.
 */
$file = fopen($fileName, 'w+');
/**
 * Add some text into the file.
 */
fwrite($file, 'Paragraph 1');
/**
 * Add a line break into the file.
 */
fwrite($file, "\r\n");
/**
 * Add some more text into the file.
 */
fwrite($file, 'Paragraph 2');
/**
 * You can use both the fsync() or fdatasync() functions 
 * to commit changs to disk.
 */
fsync($file); // or fdatasync($file).
/**
 * Close the open file pointer.
 */
fclose($file);

?>

PHP 8.1 新增了對字串鍵陣列解包的支援。為了解壓陣列,PHP 使用展開(…)運算子。PHP 7.4 中引入了這個運算子來合併兩個或多個陣列,但語法更簡潔。但在 PHP 8.1 之前,展開運算子僅支援帶數字鍵的陣列。請參閱以下程式碼片段以瞭解如何將展開運算子用於字串鍵控陣列。

<?php

/**
 * Declare an array
 */
$fruits1 = ['Jonathan Apples', 'Sapote'];
/**
 * Declare another array
 */
$fruits2 = ['Pomelo', 'Jackfruit'];
/**
 * Merge above two arrays using array unpacking.
 */
$unpackedFruits = [...$fruits1, ...$fruits2, ...['Red Delicious']];
/**
 * Print the above unpacked array.
 * This will print:
 * array(5) {
 * [0]=>
 * string(15) "Jonathan Apples"
 * [1]=>
 * string(6) "Sapote"
 * [2]=>
 * string(6) "Pomelo"
 * [3]=>
 * string(9) "Jackfruit"
 * [4]=>
 * string(13) "Red Delicious"
 * }
 */
var_dump($unpackedFruits);

?>

PHP 8.1 新增full_path了對$_FILES全域性變數中新鍵的支援。在 PHP 8.1 之前,$_FILES沒有儲存到伺服器的相對路徑或確切目錄。因此,您無法使用 HTML 檔案上傳表單上傳整個目錄。新full_path鍵解決了這個問題。它儲存相對路徑並在伺服器上重建確切的目錄結構,使目錄上傳成為可能。請參閱以下程式碼片段以瞭解如何將full_path鍵與$_FILES全域性變數一起使用。

<?php

/**
 * Check if the user has submitted the form.
 */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    /**
     * Print the $_FILES global variable. This will display the following:
     * array(1) {
     *   ["myfiles"]=> 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);
}

?>

<form action="" method="POST" enctype="multipart/form-data">
    <input name="myfiles[]" type="file" webkitdirectory multiple />
    <button type="submit">Submit</button>
</form>

PHP 8.1 新增了對新IntlDatePatternGenerator類的支援。在 PHP 8.1 之前,只能使用IntlDateFormatter。它支援昨天、今天和明天使用的八種預定義格式。但是這些格式不像IntlDatePatternGenerator。這個類允許指定日期、月份和時間的格式,並且順序將由類自動處理。請參閱以下程式碼片段以瞭解如何使用 IntlDatePatternGenerator 類。

<?php

/**
 * Define a default date format.
 */
$skeleton = "YYYY-MM-dd";
/**
 * Parse a time string (for today) according to a specified format.
 */
$today = \DateTimeImmutable::createFromFormat('Y-m-d', date('Y-m-d'));
/**
 * ===========================
 * PRINTING DATE IN USA FORMAT
 * ===========================
 * Initiate an instance for the IntlDatePatternGenerator class
 * and provide the locale information.
 * In the below example, I've used locale: en_US.
 */ 
$intlDatePatternGenerator = new \IntlDatePatternGenerator("en_US");
/**
 * Get the correct date format for the locale: en_US.
 * Following function "getBestPattern" will return:
 * MM/dd/YYYY
 */
$enUSDatePattern = $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";

?>

點贊!您已經完成了 PHP 8.1 提供的功能的學習。現在您可以繼續並開始在您當前或即將進行的專案中實現上述功能。


原文:levelup.gitconnected.com/top-10-ph...

本作品採用《CC 協議》,轉載必須註明作者和本文連結