Navigate back to the homepage

What’s New in PHP 7.4

Vishnu Damwala
November 20th, 2019 · 2 min read

PHP 7.4 coming with all new features and updates on November 28th, 2019.

Features, updates, and deprecations

  • Typed Properties 2.0
  • Null Coalesce Equal (??=) Operator
  • Numeric Literal Separator
  • Spread Operator in Array Expression
  • Support for WeakReferences
  • Support for Arrow Functions

Typed Properties 2.0

  • PHP 7 greatly increased the power of PHP’s type system with an introduction of scalar types and return types. PHP 7.4 comes with declaration types for class properties, which is missing in previous PHP versions where developers have to instead use getter and setter methods to enforce type contracts.
  • By using typed properties, you can easily declare type hints to the class variables and properties. PHP RFC for Typed Properties 2.0
  • Also, static properties will also have declare types that were not allowed earlier, following the same declaration methods for class variables and properties.

PHP7.4 Adds Support For Runtime-Enforced Type Annotations For Declared Properties.

Without Typed Properties

1/**
2 * Book class
3 */
4class Book
5{
6 /**
7 * Book id
8 *
9 * @var int $id
10 */
11 private $id;
12
13 /**
14 * Book title
15 *
16 * @var string $title
17 */
18 private $title;
19
20 /**
21 * Book constructor
22 *
23 * @param integer $id
24 * @param string $title
25 */
26 public function __construct(int $id, string $title)
27 {
28 $this->id = $id;
29 $this->title = $title;
30 }
31
32 /**
33 * Function to get book id
34 *
35 * @return integer
36 */
37 public function getId(): int
38 {
39 return $this->id;
40 }
41
42 /**
43 * Function to set book id
44 *
45 * @param integer $id
46 * @return void
47 */
48 public function setId(int $id): void
49 {
50 $this->id = $id;
51 }
52
53 /**
54 * Function to get book title
55 *
56 * @return string
57 */
58 public function getTitle(): string
59 {
60 return $this->title;
61 }
62
63 /**
64 * Function to set book title
65 *
66 * @param string $title
67 * @return void
68 */
69 public function setTitle(string $title): void
70 {
71 $this->title = $title;
72 }
73}

With Typed Properties

1/**
2 * Book class
3 */
4class Book
5{
6 /**
7 * Book id
8 *
9 * @var int $id
10 */
11 private int $id;
12
13 /**
14 * Book title
15 *
16 * @var string $title
17 */
18 private string $title;
19
20 /**
21 * Book constructor
22 *
23 * @param integer $id
24 * @param string $title
25 */
26 public function __construct(int $id, string $title)
27 {
28 $this->id = $id;
29 $this->title = $title;
30 }
31}

Null Coalesce Equal (??=) Operator

  • PHP 7.4 has also taken a null coalescing operator (??) to the next level after its introduction in PHP7.
  • This RFC proposes the introduction of a new null coalescing assignment operator (??=). Coalescing operator(??) being a comparison operator, this new coalesce equal or ??= operator is an assignment operator.

If The Left Parameter Is Null, Assigns The Value Of The Right Parameter To The Left One. If The Value Is Not Null, Nothing Is Made.

Without Null Coalesce Equal (??=) Operator

1$this->request->data['user']['password'] = $this->request->data['user']['password']
2 ?? '#EastOrWestIndiaIsBest';

With Null Coalesce Equal (??=) Operator

1$this->request->data['user']['password'] ??= '#EastOrWestIndiaIsBest';

Numeric Literal Separator

  • We humans are not optimized for parsing long sequences of digits quickly. Lack of visual separators takes longer to read and debug the code which indirectly leads to unintended errors.
  • This RFC proposes a feature to improve code readability. PHP 7.4 comes with a visual separator to solve it by supporting an underscore in numeric literals for separating groups of digits. PHP RFC for Numeric Literal Separator
1$companyMarketCap = 1_000_000_000; // a billion!
2$amount = 124_00; // $124 as cents

Enable Improved Code Readability By Supporting An Underscore In Numeric Literals To Visually Separate Groups Of Digits.

  • Underscore separators can be used in all-numeric literal notations supported by PHP:
12.124_083e-8; // float
224_702_748; // decimal
30xCAFE_F00D; // hexadecimal
40b0101_1111; // binary
50137_041; // octal

Array spread operator

  • PHP already supports Argument Unpacking (aka Spread Operator) since 5.6. PHP7.4 brings this feature to the array expression proposed in this RFC.

An Array Pair Prefixed By … Will Be Expanded In Places During Array Definition. Only Arrays And Objects Who Implement Traversable Can Be Expanded.

1$colors = ['red', 'green'];
2$moreColors = ['blue', 'orange', ...$colors, 'yellow'];
3// ['blue', 'orange', 'red', 'green', 'yellow'];

Support for WeakReferences

1$object = new stdClass;
2$weakRef = WeakReference::create($object);
3
4var_dump($weakRef->get()); // object(stdClass)#1 (0) {}
5
6unset($object);
7
8var_dump($weakRef->get()); // null

First, var_dump returns an object, and after unsetting it, second var_dump returns null.

Weak References Allow The Programmer To Retain A Reference To An Object Which Does Not Prevent The Object From Being Destroyed; They Are Useful For Implementing Cache Like Structures.


Support for Arrow Functions

  • For performing simple operations, Anonymous functions can be quite verbose and sometimes difficult to maintain in PHP.
  • Anonymous functions using simple closures makes code hard to read and understand due to its syntactic boilerplate, and manually importing used variables.
  • PHP 7.4 introduces Arrow Functions inspired from ES6. Arrow functions are short closures for performing simpler operations which improves code readability & understandability proposed in this RFC.

Without Arrow Function

1//For finding cube for given array elements without arrow function
2function cube($n){
3 return ($n * $n * $n);
4}
5
6$array = [1, 2, 3, 4, 5];
7echo "<pre>";
8print_r(array_map('cube', $array));
9echo "</pre>";

With Arrow Function

1//For finding cube for given array elements using arrow function
2$array = [1, 2, 3, 4, 5];
3echo "<pre>";
4print_r(array_map(fn($n) => $n * $n * $n, $array));
5echo "</pre>";

Output

1[1, 8, 27, 64, 125]

Deprecations in PHP 7.4

In PHP 7.4, following functionalities are deprecated, proposed in this RFC and will be removed in PHP 8.

  • The real type
  • Magic quotes legacy
  • array_key_exists() with objects
  • FILTER_SANITIZE_MAGIC_QUOTES filter
  • Reflection export`() methods
  • mb_strrpos() with encoding as 3rd argument
  • implode() parameter order mix
  • Unbinding $this from non-static closures
  • hebrevc() function
  • convert_cyr_string() function
  • money_format() function
  • ezmlm_hash() function
  • restore_include_path() function
  • allow_url_include ini directive

More articles from MeshWorld

indexOf() Method in Javascript

JavaScript array indexOf() method returns the index of first occurrence at which a given element can be found in the array, or -1 if it is not present

October 18th, 2019 · 1 min read

Install Postman native app in Ubuntu

Install Postman native app in Ubuntu. Simplify workflows and create APIs faster with Postman, a collaboration platform for API development.

October 13th, 2019 · 1 min read
© 2017–2020 MeshWorld
Link to $https://twitter.com/meshworldindiaLink to $https://facebook.com/meshworldindiaLink to $https://instagram.com/meshworld.india/