{"id":9209,"date":"2020-11-28T04:02:00","date_gmt":"2020-11-28T09:02:00","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=9209"},"modified":"2021-12-09T04:27:14","modified_gmt":"2021-12-09T09:27:14","slug":"php-8-release-available","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/php-8-release-available\/","title":{"rendered":"PHP 8 Release out With a Host of New Features And Optimizations"},"content":{"rendered":"\n

The PHP<\/a> development team announced the release of PHP 8 which has been the much-awaited version of the popular open-source<\/a> and cross-platform<\/a> web programming language<\/p>\n\n\n\n

PHP 8.0 is the latest major update of the PHP language. It comes with many new features and optimizations over preceding versions. These include named arguments, union types, attributes, constructor property promotion, match expression, nullsafe<\/em><\/strong> operator, Just In Time Compilation (JIT), and improvements in the type system, error handling, and consistency.<\/p>\n\n\n\n

Here are Some Comparisons of PHP 8 Release Vs PHP 7.<\/p>\n\n\n\n

PHP 8 Named Arguments<\/h2>\n\n\n\n
\/\/ PHP 7\nhtmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);\n\n\/\/ PHP 8\n\/\/ You can specify only required parameters, skipping optional ones, they order-independent and self-documented.\nhtmlspecialchars($string, double_encode: false);<\/code><\/pre>\n\n\n\n

PHP 8 Union Types<\/h2>\n\n\n\n

Instead of PHPDoc annotations for a combination of types, you can use native union type declarations that are validated at runtime.<\/p>\n\n\n\n

\/\/ PHP 7\nclass Number {\n  \/** @var int|float *\/\n  private $number;\n\n  \/**\n   * @param float|int $number\n   *\/\n  public function __construct($number) {\n    $this->number = $number;\n  }\n}\n\nnew Number('NaN'); \/\/ Ok\n\n\/\/ PHP 8\nclass Number {\n  public function __construct(\n    private int|float $number\n  ) {}\n}\n\nnew Number('NaN'); \/\/ TypeError<\/code><\/pre>\n\n\n\n

PHP 8 Attributes<\/h2>\n\n\n\n

Instead of PHPDoc annotations, you can now use structured metadata with PHP\u2019s native syntax.<\/p>\n\n\n\n

\/\/ PHP 7\nclass PostsController\n{\n    \/**\n     * @Route("\/api\/posts\/{id}", methods={"GET"})\n     *\/\n    public function get($id) { \/* ... *\/ }\n}\n\n\/\/ PHP 8\nclass PostsController\n{\n    #[Route("\/api\/posts\/{id}", methods: ["GET"])]\n    public function get($id) { \/* ... *\/ }\n}<\/code><\/pre>\n\n\n\n

PHP 8 Constructor Property Promotion<\/h2>\n\n\n\n

Less boilerplate code to define and initialize properties.<\/p>\n\n\n\n

\/\/ PHP 7\nclass Point {\n  public float $x;\n  public float $y;\n  public float $z;\n\n  public function __construct(\n    float $x = 0.0,\n    float $y = 0.0,\n    float $z = 0.0,\n  ) {\n    $this->x = $x;\n    $this->y = $y;\n    $this->z = $z;\n  }\n}\n\n\/\/ PHP 8\nclass Point {\n  public function __construct(\n    public float $x = 0.0,\n    public float $y = 0.0,\n    public float $z = 0.0,\n  ) {}\n}<\/code><\/pre>\n\n\n\n

PHP 8 Nullsafe operator<\/h2>\n\n\n\n

Instead of null check conditions, you can now use a chain of calls with the new nullsafe<\/strong><\/em> 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.<\/p>\n\n\n\n

\/\/ PHP 7\n$country = null;\n\nif ($session !== null) {\n  $user = $session->user;\n\n  if ($user !== null) {\n    $address = $user->getAddress();\n\n    if ($address !== null) {\n      $country = $address->country;\n    }\n  }\n}\n\n\/\/ PHP 8\n$country = $session->user->getAddress()->country;<\/code><\/pre>\n\n\n\n

PHP 8 Match expression<\/h2>\n\n\n\n

The new match is similar to switch and has the following features:<\/p>\n\n\n\n