New Features and Improvements in PHP 7.4

PHP 7.4, the latest PHP 7 minor release, has brought with it lots of new features, syntax additions and fixes. So, without any further ado, let’s start to review all the changes in the latest version.

  1. Arrow functions’ support

Mainly applied in JavaScript, anonymous functions seem to be verbose in PHP. Their implementation and maintenance procedures are also quite difficult and more complex. 

Arrow functions, aka “short closures”, allow for less verbose one-liner functions. It will enable PHP developers to write simple and neat code. This will not only result in a higher level of code readability and simplicity but will also make the development process go faster, thus saving you time.

  1. Typed properties’ support

After long absence, typed properties are finally back in PHP. One of the most important updated PHP features, by using them, programmers can easily declare type hints to the class variables and properties (including static properties). Before, it was not possible and programmers had to create getter and setter methods to enforce type contracts.

All types are supported, (with the exception of void (because it is not useful and has unclear semantics) and callable (because its behavior is context dependent)) including int, float, array, string, nullable types, object and so forth. 

If you try to assign an irrelevant value from the type, for e.g., declaring $name as string, then you will get a TypeError message.

Like arrow functions, typed properties also let you make your code shorter and cleaner.

  1. Preloading

The main purpose of preloading is to increase the performance of the latest PHP release. In simple words, preloading is the process of loading libraries and frameworks in OPcache and is one of my favorite addition to PHP 7.4. For instance, if you use a framework, its files have to be loaded and linked on every request.

When configuring OPcache, initially these code files participate in the request processing and then they are checked for changes each time. Preloading allows the server to load the specified PHP files into shared memory on startup, and have them permanently available to all subsequent requests without additional checks for file changes.

Another thing that needs to be mentioned here is that during preloading, PHP also eliminates needless includes and resolves class dependencies and links with traits, interfaces, and more.

Now as mentioned above, it should bring a significant boost in performance but all this does comes at a cost: preloaded files remain cached in OPcache memory forever so if their source is changed, the server has to be restarted.

  1. Covariant returns & contravariant parameters

Previously, PHP had mostly invariant parameter and return types which presented some constraints. This has changed with PHP 7.4, which introduces the covariant (types are ordered from more specific to more generic) returns and contravariant (it reverses the order, from more generic to more specific) parameters, with which you’ll be able to change the parameter’s type to one of its supertypes. The returned type, in turn, can be easily replaced by its subtype.

  1. Weak References

In the latest release, the WeakReference class allows you to save a link to an object which doesn’t prevent it from being destroyed. At the moment, PHP supports weak reference by using an extension like pecl-weakref. Anyway, don’t confuse it with the WeakRef class of the Weakref extension. Due to this feature, they can more easily implement cache-like structures.

  1. Coalescing assign operator

This is another cool feature that comes with the latest release. It comes in handy when a programmer need to apply a ternary operator together with isset(). This will enable them to return the 1st operand if it exists and is not NULL. If not, it will just return the 2nd operand.

  1. A spread operator in array expression

The latest release enables you to use spread operators in arrays that are faster compared to array_merge. There are two main reasons for that: 

  • One, spread operator is a language structure while array_merge is a function. 
  • Two, compile-time can be optimized for constant arrays.

Thus, resulting in increased PHP 7.4 performance. Although, PHP has supported spread operators since its version 5.6, but now they are available for array expressions too. Also, it will be possible to expand the same array multiple times. Moreover, since normal elements can be added before or after it, you’ll be able to use its syntax in the array.

One thing that needs to be mentioned here is that its not a replacement for array_merge. It can be useful in certain situations, but not usable in others.

  1. A new custom object serialization mechanism

The latest release ships with the following new magic methods:

  • __serialize
  • __unserialize

Previous serialization mechanism using Serializable interface and __sleep() & __wakeup() magic methods, had a number of issues that lead to complex and unreliable code. Combining the versatility of the Serializable interface with the approach of implementing __sleep / __ wakeup methods, the new magic methods will allow you to avoid these issues.

  1. Reflection for references

Libraries like symfony/var-dumper heavily rely on ReflectionAPI to accurately display variables. Before, it wasn’t possible to properly reflect references, which forced these libraries to use some hacky techniques to detect references. The latest release adds the ReflectionReference class which solves this issue.

  1. Support for throwing exceptions from __toString()

Before, exceptions could not be thrown in __toString method. Because the conversion of objects to strings is performed in many methods of the standard library and not all of them are ready to “process” exceptions correctly. Well, things have changed and exceptions can now be thrown from __toString.

  1. Small deprecations

Several language constructs and functions are marked as deprecated, resulting in a E_DEPRECATED warning when used. Since most of the deprecations are based on inconsistencies and clean ups, so there is nothing very astounding. Following is the list of deprecations:

  • The real type
  • array_key_exists() with objects
  • allow_url_include ini directive
  • Reflection export() methods
  • ezmlm_hash() function
  • implode() parameter order mix
  • convert_cyr_string() function
  • And more.
  1. Removed extensions

A number of extensions have been unbundled:

  • ext/interbase: this extension to support InterBase database has serious implementation issues and has been moved to PECL RFC
  • ext/wddx: this data format intended for exchanging data had its own extension, now deprecated and moved to PECL RFC
  • ext/recode: an extension to convert between charsets. It is old, discontinued, and iconv and mbstring extensions are doing the same in PHP RFC

Final words

In this post, we covered a good number of changes and additions that came with the release of the latest version. More powerful as compared to its predecessors, PHP 7.4 comes with plenty of new PHP features that reduce memory usage and greatly increases its performance. With this latest release, you’ll gain the ability to avoid some previous limitations of PHP, write cleaner code, and create modern web applications faster. So, what are you waiting for, start coding!

Leave a Comment

The reCAPTCHA verification period has expired. Please reload the page.