Home  >  Article  >  The top ten new features of PHP8.1, use them quickly!

The top ten new features of PHP8.1, use them quickly!

藏色散人
藏色散人forward
2021-12-08 14:05:584566browse

I'll walk through the top 10 features offered by PHP 8.1 so you can start using them in your projects and improve your PHP experience. Beginners and experienced developers can benefit from this article.

Top 10 functions provided by PHP 8.11. Enumeration

2.Fiber

3.

never

Return type4.

readonly

Property5.

final

Class constant6.New The

array_is_list()

function7. The new

fsync()

and fdatasync()function8. Right Support for string key array unpacking

9.

$_FILES

New full_pathkey for directory upload10.New

IntlDatePatternGenerator

Class

1. EnumerationPHP 8.1 added support for enumeration, abbreviated as

enum

. It is an itemized type containing a fixed number of possible values. See the following code snippet to learn how to use enumerations.

' 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 adds support for

Fiber

, a low-level component that allows concurrent code execution in PHP. A Fiber is a block of code that contains its own variables and state stack. These fibers can be thought of as application threads and can be started from the main program. Once started, the main program cannot suspend or terminate Fiber. It can only be paused or terminated from within a Fiber code block. After Fiber is suspended, control is again returned to the main program, and it can continue executing Fiber from the point of suspension.

Fiber itself does not allow multiple Fibers or the main thread and a Fiber to be executed at the same time. However, it is a huge advantage for a PHP framework to efficiently manage the execution stack and allow asynchronous execution.

See the following code snippet to learn how to use 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

Return typePHP 8.1 added a return type named

never

. The never type can be used to indicate that a function will terminate program execution after performing a specified set of tasks. This can be done by throwing an exception, calling the exit() or die() function.

never

The return type is similar to the void return type. However, the void return type continues execution after the function has completed a specified set of tasks.

See the following code snippet to understand how to use the never return type.

     * @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

AttributePHP 8.1 added a class attribute named

readonly

. A class property that has been declared read-only can only be initialized once. The values ​​set inside cannot be changed. If you try to force update the value, the application will throw an error. See the following code snippet to learn how to use read-only properties.

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 
     * @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;

?>
5.

final

Class constantsPHP 8.1 added support for class constants named

final

. Final class constants cannot be modified, even through inheritance, which means they cannot be extended or overridden by subclasses.

This flag cannot be used for private constants because it cannot be accessed outside the class. Declaring final and private constants will result in a fatal error.

See the following code snippet to understand how to use the final flag.

6. New

array_is_list()

FunctionPHP 8.1 adds an array function named

array_is_list()

. It identifies whether the specified array has all consecutive integers starting at 0. This function returns true if the array is a semantic list of values ​​(an array whose keys start at 0, are all integers, and have no gaps between them). It also returns true for empty arrays. See the following code snippet to learn how to use the array_is_list() function.

 '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']);

?>
Arrays in which the keys are not 0-based, or the keys are not integers, or the keys are integers but do not appear in order will evaluate to 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. New

fsync()

and fdatasync() functions PHP 8.1 added support for

fsync()

and fdatasync() function support. Both have similarities to the existing fflush() function, which is currently used to flush buffers into the operating system. However, fsync() and fdatasync() flush this buffer to physical storage. The only difference between them is that the fsync() function includes metadata when synchronizing file changes, while the fdatasync() function does not include metadata.

fsync()函数将采用文件指针并尝试将更改提交到磁盘。成功时返回 true,失败时返回 false,如果资源不是文件,则会发出警告。fdatasync()函数的工作方式相同,但速度稍快一些,因为 fsync() 将尝试完全同步文件的数据更改和有关文件的元数据(上次修改时间等),这在技术上是两次磁盘写入。

请参阅以下代码片段以了解如何使用 fsync() 和 fdatasync() 函数。

8. 对字符串键数组解包的支持

PHP 8.1 添加了对字符串键数组解包的支持。为了解压数组,PHP 使用展开(…)运算符。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

PHP 8.1 添加了对$_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

PHP 8.1 添加了对新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";

?>

点赞!您已经完成了 PHP 8.1 提供的功能的学习。现在您可以继续并开始在您当前或即将进行的项目中实现上述功能。


原文:https://levelup.gitconnected.com/top-10-php-8-1-features-you-should-start-using-now-7161b91275fd

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete