Before we start, What is PHP?
PHP is a general-purpose scripting language especially suited to web development. Danish-Canadian programmer Rasmus Lerdorf created it in 1994.
The PHP Group now produces the PHP reference implementation. PHP was originally used for Personal Home Page, but now it is used for the recursive initialism PHP: Hypertext Preprocessor.
PHP is a programming language and widely-used open source general-purpose scripting language that is particularly suited for web development and installed into HTML. All WordPress sites are made with PHP, too.
Now, What’s PHP 8.0.0?
PHP 8.0.0 is a new update of the PHP programming language that launched in 26th November, 2020. It has many new features and optimization, including:
- Named Arguments
- Null-safe Operator
- Union Types
- Constructor Property Promotion
- Match Expression
- JIT (Just in Time Compiler)
It also has many more improvements in error handling, the type system, consistency and syntax.
Let us go deeper and explain every new feature and its advantages.
Named arguments allow you to fill in values of a function by defining the value name. So, that you don’t have to analyze their order, and you can also skip optional parameters!
The null-safe operator provides safety in method/property chaining, when the return value or property can be null.
The ?-> null-safe operator short-circuits the rest of the expression if it encounters a null value and immediately returns null without causing any errors.
Attributes allow declaring meta-data for functions, classes, properties, and parameters. Attributes map to PHP class names (declared with an Attribute itself), and they can be fetched programmatically with PHP Reflection API.
Attributes make it easy and well-performed to declare Attributes/ annotations that required storing them in DocBlock comments previously and parsing the string to infer them.
Union types are a combination of types (two or more) that show that either one can be used. Because void indicates “no return value at all,” it cannot be part of a union type.
Moreover, can nullable unions be written using | null, or by using the existing?
Union Types extend type declarations (return types, parameters, and class properties) to declare more than one type.
It also supports false as a special type (for Boolean false), a prevalent trait in legacy code that don’t use Exceptions.
Constructor Property Promotion
A new syntax indicates class features right from the class constructor (__construct magic method).
In constructor, PHP 8.0 supports declaring the visibility (public, private, or protected) and type. Those properties will be read as class properties with the same visibility and type they are declared in constructor.
The match can return values, don’t require break statements, combine conditions, use strict type comparisons, and don’t do any coercion.
Match expressions are similar to switch blocks, but match blocks present type-safe comparisons which promote a return value that doesn’t need break-out and supports many matching values. It also guarantees that at least one part is matched, securing all cases are accounted for.
Not all switch blocks might convert well to match blocks. Code requires backward-compatibility switch blocks with various statements (as opposed to single-line expressions) or demands fall through functionality that still fits the switch statements.
JIT (Just in Time Compiler)
JIT: This technique will collect parts of the code at runtime to use the selected version instead.
PHP Opcache supports JIT. It’s disabled by default, and if allowed, JIT collects and stores native instructions. It doesn’t make a remarkable variation in IO-bound web applications but presents a review boost for CPU-heavy applications.
Note that JIT is still fresh and has bug fixes because it was released a day before PHP 8.0.0. It makes debugging and profiling more complex with the added additional cover
A WeakMap lets you store and connect random values for object keys, but without stopping the garbage collector from removing it, the object falls out of scope everywhere else.
Built upon the weak refs RFC added in PHP 7.4, a WeakMap implementation is added in PHP 8. WeakMap contains sources to objects, which don’t stop those objects from being garbage collected.
What do WordPress users need to know about PHP 8.0.0?
We know that WordPress users used PHP latest version before PHP 8.0.0. It was perfect and has many improvements for them, but PHP 8.0.0 is more modern and it’s a massive change from the previous versions.
So, should they upgrade now?
No. The future version of WordPress, 5.6, is expected to be “beta compatible with PHP 8. This means that most core WordPress functionality will work, but unforeseen bugs may still happen for some time, even without the appearance of additional plugins or themes.
WordPress is called for a new testing with PHP 8 to discover and fix as many remaining bugs as possible.
A huge number of WordPress plugins and themes won’t fit PHP 8 directly. Those who don’t run into deadly errors during regular usage may still see impulsive behavior for some time.
Does PHP 8.0.0 include any breaking changes?
Some developers argued that PHP is unsafe by default. While this is up for discussion, it’s true that PHP versions before PHP 8 are more fault-tolerant and try to guarantee that code will run even if minor errors are present.
PHP 8 uses much more strict typing than previous versions. Many built-in functions are now selective about their input, and PHP 8 itself is more stringent about how input is passed to functions. Now, Issues that previously appeared in notices appear in warnings, and issues that previously appeared in warnings appear in errors.
Some functions and points that were objected to in PHP 7.x have been removed. These include:
- The $php_errormsg variable
- The create_function() function
- The mbstring.func_overload ini directive
- The real type
- The allow_url_include ini directive
- The restore_include_path() function
- The each() function
All PHP code should be fit with PHP 8.0 by two years minimum or danger of being stuck on a possibly weak PHP version.