Whats New in PHP 8?
PHP was released on November 26th 2020, it is a major version and has a number of changes from previous versions of PHP.
Some new key features that have been released in PHP 8 we will examine below include:
- Just-in-time (JIT) Compiler
- Support for Union types
- Named arguments
It should also be noted that the following software does not currently work with PHP 8:
- JSON Extension (Functionality now included in PHP 8 by default
- XML RPC has been unbundled in PHP 8 but can still be installed using PECL
- If you are using cPanel/WHM there is currently no support for using DSO as an apache handler with PHP 8
A closer look at JIT (Just In Time Compiler)
JIT is arguably the biggest change in PHP 8, in order to understand how it works you need to understand compilation. Compilation is a translation of human-readable code to machine code, translating human-readable code to machine code can happen in three different ways: Ahead Of Time (e.g C++) , Just In Time or Interpretation.
PHP has traditionally been an interpreted language however it is now not only interpreted but also Just In Time compiled. Capable of compiling this intermediate representation of code into binary objects in runtime operating in a hybrid mode where code is partially compiled and interpreted.
When using JIT in PHP 8, parts of your php code will be compiled into machine code so your PHP handler won’t interpret these parts anymore. Your php code will talk directly to the CPU thus improving performance.
Other new features with PHP 8
Arbitary Union Types
Union types accept values of multiple different types. they allow passing arguments to a function based on the parameter name. PHP previously supported two union types you are probably familar with (Type or Null and array or Traversable). PHP 8 brings support for arbitary union types that accept values of multiple different types instead of a single one.
Named arguments allow passing arguments to a function based on the parameter name rather than the parameter position. This means the meaning of the argument is self-documenting amd makes arguments order-independent, allowing the skipping of default values arbitrarily.
Weak maps allow creation of a map from objects to arbitrary values without stopping the objects used as keys from garbage collection If an object key is garbage-collected, it will be removed from the map. Weak maps are normally used to associate data with individual object instances without requiring that they stay alive and effectively leak memory in long-running processes.
Constructor property promotion
Constructor property promotion introduces a short-hand syntax that allows the combining of the definition of properties and the constructor. This capability addresses a situation in which the definition of simple value objects has required a lot of boilerplate code, because all properties need to be repeated at least four times.