Skip to main content
 Web开发网 » 站长学院 » 浏览器插件

php8正式发布了,

2021年10月25日6740百度已收录

PHP8包含了许多新功能和优化,性能得到了进一步的提升,具体可参考官方原文介绍如下:

Named arguments RFCPHP 7

htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);PHP 8

htmlspecialchars($string, double_encode: false);Specify only required parameters, skipping optional ones.Arguments are order-independent and self-documented.Attributes RFCPHP 7

class PostsController{    /**     * @Route("/api/posts/{id}", methods={"GET"})     */    public function get($id) { /* ... */ }}PHP 8

class PostsController{    #[Route("/api/posts/{id}", methods: ["GET"])]    public function get($id) { /* ... */ }}Instead of PHPDoc annotations, you can now use structured metadata with PHP's native syntax.

Constructor property promotion RFC DocPHP 7

class Point {  public float $x;  public float $y;  public float $z;  public function __construct(    float $x = 0.0,    float $y = 0.0,    float $z = 0.0,  ) {    $this->x = $x;    $this->y = $y;    $this->z = $z;  }}PHP 8

class Point {  public function __construct(    public float $x = 0.0,    public float $y = 0.0,    public float $z = 0.0,  ) {}}Less boilerplate code to define and initialize properties.

Union types RFC DocPHP 7

class Number {  /** @var int|float */  private $number;  /**   * @param float|int $number   */  public function __construct($number) {    $this->number = $number;  }}new Number('NaN'); // OkPHP 8

class Number {  public function __construct(    private int|float $number  ) {}}new Number('NaN'); // TypeErrorInstead of PHPDoc annotations for a combination of types, you can use native union type declarations that are validated at runtime.

Match expression RFC DocPHP 7

switch (8.0) {  case '8.0':    $result = "Oh no!";    break;  case 8.0:    $result = "This is what I expected";    break;}echo $result;//> Oh no!PHP 8

echo match (8.0) {  '8.0' => "Oh no!",  8.0 => "This is what I expected",};//> This is what I expectedThe new match is similar to switch and has the following features:

Match is an expression, meaning its result can be stored in a variable or returned.Match branches only support single-line expressions and do not need a break; statement.Match does strict comparisons.Nullsafe operator RFCPHP 7

$country =  null;if ($session !== null) {  $user = $session->user;  if ($user !== null) {    $address = $user->getAddress();      if ($address !== null) {      $country = $address->country;    }  }}PHP 8

$country = $session?->user?->getAddress()?->country;Instead of null check conditions, you can now use a chain of calls with the new nullsafe operator. When the evaluation of one element in the chain fails, the execution of the entire chain aborts and the entire chain evaluates to null.

Saner string to number comparisons RFCPHP 7

0 == 'foobar' // truePHP 8

0 == 'foobar' // falseWhen comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a string and uses a string comparison.

Consistent type errors for internal functions RFCPHP 7

strlen([]); // Warning: strlen() expects parameter 1 to be string, array givenarray_chunk([], -1); // Warning: array_chunk(): Size parameter expected to be greater than 0PHP 8

strlen([]); // TypeError: strlen(): Argument #1 ($str) must be of type string, array givenarray_chunk([], -1); // ValueError: array_chunk(): Argument #2 ($length) must be greater than 0Most of the internal functions now throw an Error exception if the validation of the parameters fails.

Just-In-Time compilationPHP 8 introduces two JIT compilation engines. Tracing JIT, the most promising of the two, shows about 3 times better performance on synthetic benchmarks and 1.5–2 times improvement on some specific long-running applications. Typical application performance is on par with PHP 7.4.

Relative JIT contribution to PHP 8 performancephp8正式发布了,  php8 第1张

Type system and error handling improvementsStricter type checks for arithmetic/bitwise operators RFCAbstract trait method validation RFCCorrect signatures of magic methods RFCReclassified engine warnings RFCFatal error for incompatible method signatures RFCThe @ operator no longer silences fatal errors.Inheritance with private methods RFCMixed type RFCStatic return type RFCTypes for internal functions Email threadOpaque objects instead of resources for Curl, Gd, Sockets, OpenSSL, XMLWriter, and XML extensionsOther syntax tweaks and improvementsAllow a trailing comma in parameter lists RFC and closure use lists RFCNon-capturing catches RFCVariable Syntax Tweaks RFCTreat namespaced names as single token RFCThrow is now an expression RFCAllow ::class on objects RFCNew Classes, Interfaces, and FunctionsWeak Map classStringable interfacestr_contains(), str_starts_with(), str_ends_with()fdiv()get_debug_type()get_resource_id()token_get_all() object implementation

评论列表暂无评论
发表评论
微信