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 will come 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.

PHP7.4 adds support for runtime-enforced type annotations for declared properties.

PHP RFC: 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.

Without Typed Properties
With Typed Properties

Null Coalesce Equal (??=) Operator

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

PHP RFC: Null Coalescing Assignment Operator
Without Null Coalesce Equal (??=) Operator
With Null Coalesce Equal (??=) Operator

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.

Enable improved code readability by supporting an underscore in numeric literals to visually separate groups of digits.

PHP RFC: Numeric Literal Separator

Underscore separators can be used in all-numeric literal notations supported by PHP:


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.

PHP RFC: Spread Operator in Array Expression

Support for WeakReferences

PHP 7.4 introduces the WeakReference class. Weak references are references to objects, which do not prevent them from being destroyed proposed in this RFC.

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.

PHP RFC: Weak References

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
With Arrow Function

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
Reference links

LEAVE A REPLY

Please enter your comment!
Please enter your name here