PHP 8 is now Released! – Laravel News

The PHP development team announced the release of PHP 8 yesterday:

PHP 8.0 is a major update of the PHP language.It contains many new features and optimizations including named arguments, union types, attributes, constructor property promotion, match expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency.

Here is the list of mai…….

npressfetimg-4600.png

The PHP development team announced the release of PHP 8 yesterday:

PHP 8.0 is a major update of the PHP language.
It contains many new features and optimizations including named arguments, union types, attributes, constructor property promotion, match expression, nullsafe operator, JIT, and improvements in the type system, error handling, and consistency.

Here is the list of main new features:

  • Union Types
  • Named Arguments
  • Match Expressions
  • Attributes
  • Constructor Property Promotion
  • Nullsafe Operator
  • Weak Maps
  • Just In Time Compilation
  • And much much more…

Here are some of the highlights from the announcement:

PHP 8 Named arguments

1// PHP 7

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

3

4// PHP 8

5// Specify only required parameters, skipping optional ones.

6// Arguments are order-independent and self-documented.

7htmlspecialchars($string, double_encode: false);

PHP 8 Attributes

Instead of PHPDoc annotations, you can now use structured metadata with PHP’s native syntax.

1// PHP 7

2class PostsController

3{

4 /**

5 * @Route("/api/posts/{id}", methods={"GET"})

6 */

7 public function get($id) { /* ... */ }

8}

9

10// PHP 8

11class PostsController

12{

13 #[Route("/api/posts/{id}", methods: ["GET"])]

14 public function get($id) { /* ... */ }

15}

PHP 8 Constructor property promotion

Less boilerplate code to define and initialize properties.

1// PHP 7

2class Point {

3 public float $x;

4 public float $y;

5 public float $z;

6

7 public function __construct(

8 float $x = 0.0,

9 float $y = 0.0,

10 float $z = 0.0,

11 ) {

12 $this->x = $x;

13 $this->y = $y;

14 $this->z = $z;

15 }

16}

17

18

19// PHP 8

20class Point {

21 public function __construct(

22 public float $x = 0.0,

23 public float $y = 0.0,

24 public float $z = 0.0,

25 ) {}

26}

PHP 8 Union types

Instead of PHPDoc annotations for a combination of types, you can use native union type declarations that are validated at runtime.

1// PHP 7

2class Number {

3 /** @var int|float */

4 private $number;

5

6 /**

7 * @param float|int $number

8 */

9 public function __construct($number) {

10 $this->number = $number;

11 }

12}

13

14new Number('NaN'); // Ok

15

16// PHP 8

17class Number {

18 public function __construct(

19 private int|float $number

20 ) {}

21}

22

23new Number('NaN'); // TypeError

PHP 8 Nullsafe operator

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.

1// PHP 7

2$country = null;

3

4if ($session !== null) {

5 $user = $session->user;

6

7 if ($user !== null) {

8 $address = $user->getAddress();

9

10 if ($address !== null) {

11 $country = $address->country;

12 }

13 }

14}

15

16// PHP 8

17$country = $session?->user?->getAddress()?->country;

PHP 8 Match expression

The 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.

1// PHP 7

2switch (8.0) {

3 case '8.0':

4 $result = "Oh no!";

5 break;

6 case 8.0:

7 $result = "This is what I expected";

8 break;

9}

10echo $result;

11//> Oh no!

12

13

14// PHP 8

15echo match (8.0) {

16 '8.0' => "Oh no!",

17 8.0 => "This is what I expected",

18};

19//> This is what I expected

Of course, these are just the highlights. Check out the official release announcement for all the details.

Source: https://laravel-news.com/php-8