Let’s see difference between PHP7 and PHP8
Named arguments
Named arguments allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes the arguments order-independent, and allows skipping default values arbitrarily.
PHP7 | PHP8 |
---|---|
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, ‘UTF-8’, false); | htmlspecialchars($string, double_encode: false); |
Attributes
This RFC proposes Attributes as a form of structured, syntactic metadata to declarations of classes, properties, functions, methods, parameters and constants. Attributes allow to define configuration directives directly embedded with the declaration of that code.
PHP7 | PHP8 |
---|---|
class PostsController { /** * @Route(“/api/posts/{id}”, methods={“GET”}) */ public function get($id) { /* … */ } } | class PostsController { #[Route(“/api/posts/{id}”, methods: [“GET”])] public function get($id) { /* … */ } } |
Constructor property promotion
Currently, the definition of simple value objects requires a lot of boilerplate, because all properties need to be repeated at least four times. Consider the following simple class:
PHP7 | PHP8 |
---|---|
lass 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; } } | class Point { public function __construct( public float $x = 0.0, public float $y = 0.0, public float $z = 0.0, ) {} } |
Union types
A “union type” accepts values of multiple different types, rather than a single one. PHP already supports two special union types:
PHP7 | PHP8 |
---|---|
class Number { /** @var int|float */ private $number; /** * @param float|int $number */ public function __construct($number) { $this->number = $number; } } new Number(‘NaN’); // Ok | class Number { public function __construct( private int|float $number ) {} } n |
Comments
Post a Comment