As one of the best programming languages today, PHP offers new ways to make web development and coding more flexible. With the introduction of PHP 8.0 and PHP 8.1, PHP aims to bring more to the table with new features that offer better syntax, security, and more.
Keep reading below to learn more about what’s new with PHP 8.0 and all its special functions. Discover all the major updates and improvements from past versions and how they can affect your WordPress site.
Understanding the PHP 8.0 Update
The PHP 8.0 update offers significant changes to developers and websites globally. Released in 2020, it promised new features and improvements that generally boast better performance, syntax, type safety, and error handling through innovative request-for-comments (RFC) and other functions.
While the difference in performance is not as remarkable as the one between PHP 5 and PHP 7, PHP 8.0 provides steady improvements with each release. Plus, most changes in PHP 7 up to PHP 7.4 are mostly deprecated.
Both PHP 8.0 and PHP 8.1 introduce new functions to help developers code faster and more flexibly. The following are some of the most note-worthy offerings that developers should consider.
Just-in-time or JIT compiler is one of the best new features in the latest PHP versions. Through this RFC, developers can bypass compilation to considerably improve performance and memory use.
PHP implements this RFC as a near-independent part of Opcache. With JIT enabled, developers can run codes through the CPU instead of third-party programs like Zend VM, boosting calculation speed.
Apart from better numerical coding solutions, JIT also offers potentially better performance for “typical” PHP web application codes. Plus, the possibility of moving more code from C to PHP since PHP will now become faster through JIT.
In addition, PHP 8.0 introduces two JIT compilators: tracing and 1.5. Out of the two, the tracing JIT offers the most promise by showing about thrice the performance on synthetic benchmarks. Meanwhile, 1.5 has twice the improvement on long-running applications.
The latest PHP version provides named arguments to extend existing positional parameters. Now, developers can pass input data to a function based on the parameter name instead of position. Because of this, the named arguments offer various advantages to developers.
For one, it enables them to create more understandable codes because the meaning of the argument is self-documenting. It’s also order-independent, meaning developers won’t have to pass arguments to a function the same way as the function signature.
Moreover, it’s also possible for developers to combine arguments with positional arguments. Plus, it allows them to specify arguments they want to change. That way, they won’t have to adjust arguments if they don’t want to overwrite default values.
Named arguments are also handy with class or type declarations.
Union types 2.0
In previous PHP versions, developers could only specify union types in doc annotations. PHP didn’t provide support except for “?Type syntax” and the “iterable” class.
PHP 8.0 aims to support union types in function signatures through the union types 2.0 RFC. Through this update, developers won’t rely on inline documentation anymore.
In addition, supporting union types in PHP allows developers to move different classes of information from phpdoc into function signatures. Because of this, they can achieve various benefits, including catching mistakes early, preventing type information from being outdated, and checking types during inheritance.
Before, developers couldn’t use the null coalescing operator on method calls. They would need intermediate checks or optional helpers from third-party frameworks. However, with the new nullsafe operator, developers can have such functions on methods. It means developers can now use a chain of calls instead of null check conditions. If the evaluation of one element in the chain fails, the whole execution chain halts and the entire chain evaluates to “null.”
Commonly known as annotations in other languages, attributes are structured metadata that developers can use to specify properties for objects, elements, or files. Developers can also use this feature to add metadata to all class names without parsing doblocks.
Up until PHP 7.4, the only way to add metadata to declarations of classes was through doc-comments. With the new Attributes V2 RFC, PHP defines attributes as structured, syntactic metadata developers can use to add to declarations of types, functions, properties, methods, parameters, and constants.
Now, developers can add attributes before or after a docblock comment and have one or more of them with one or more associated values.
Match expression V2
Those familiar with “switch expressions” already know what match expressions are about. Only match expressions are a better version of “switch.”
For instance, it offers safer semantics, which allows developers to return values only once. It can also combine conditions without ‘breaking’ values. Plus, it doesn’t do any type coercion and uses strict type comparisons.
Constructor property promotion
This RFC introduces a more concise syntax to help simplify the property declaration, which aims to merge the constructor and the parameter definition. Now, developers have a better way of declaring properties or parameters by making them less redundant and shorter. This RFC also lets developers reduce the boilerplate code they write for value objects.
Saner string to number comparisons
PHP 8.0 offers this RFC to make numeric string comparisons more reasonable. To do such a task before, PHP would cast the string to a number first, then perform the comparison between integers or floats. While this practice was helpful in different ways, it could create fatal errors with site function and security implications.
As mentioned, this RFC aims to offer string-to-number comparisons a more reasonable behavior. It will use a number comparison when comparing to a numeric string or convert the number to a string and use a string comparison.
Consistent type errors for internal functions
When passing an illegal type parameter, internal and user-defined functions behave differently. Internal functions don’t throw “TypeError” like user-defined functions. Instead, they emit warnings and return them as “null.”
PHP 8.0 addresses these inconsistencies by proposing an RFC that makes internal parameter parsing APIs generate a “ThrowError” in case of a parameter type mismatch. Because of this, all internal function behaviors are more consistent.
New classes, interfaces, and functions
Among the notable new PHP 8.0 changes are the WeakMap class. Generally, a weak map is a collection of data or objects with weakly-referenced keys. Weakly referencing data means there’s no preventing them from being destroyed, resulting in poor performance and memory leaks.
PHP 7.4 supports weak references to retain a reference to data that doesn’t prevent it from being destroyed. However, raw, weak references are limited to themselves.
On the other hand, weak maps are much more common in practice. But it’s impossible to implement an efficient weak map on top of PHP weak references. With PHP 8.0’s WeakMap class, developers can do just that. They can create objects as weak map keys that developers or the program can destroy. That means PHP will consider and collect these objects as garbage if nothing references them for an extended period.
Another addition to the many helpful new PHP 8.0 features is the Stringable interface. This RFC automatically adds this function to all classes implementing the “__toString()” method. It means developers won’t have to manually encode the interface each time a class implements the code.
In previous versions of PHP, “strstr” and “strpos” were standard options for developers to look for a needle inside a string. Unfortunately, many viewed both functions as not intuitive enough and can be confusing for new PHP developers. Now, they can search inside a string more easily with the new “str_contains” function.
In addition to str_contains, PHP 8.0 offers “str_starts_with()” and “str_ends_with()” to help with better needle search inside a string.
Other new functions include “fdiv(),” “get_debug_type(),” “get resource_id(),” “token_get_all()” object implementation, and the DOM Traversal and Manipulation APIs.
Type system and error handling
PHP 8.0 also offers various system and error-handling improvements. One of them is a mixed type. A “mixed type” helps prevent missing types in the code. In PHP, missing types could mean a function returns null, one of several types, or one that can’t be type-hinted.
Generally, “mixed” could mean array, boolean, callable, int, float, null, object, resource, and string. Through the new mixed type, developers can add types to parameters, class properties, and function returns. Doing this will help indicate that the type information wasn’t forgotten but rather specified more precisely.
Another notable improvement that PHP 8.0 brings is the static return type. Before PHP 8.0, “self” could return, but “static” couldn’t. Now, with PHP’s dynamic typed nature, developers will find this feature incredibly useful.
Other type system and error handling improvements that PHP provides include abstract trait method validation, correct signatures of magic methods, inheritance with private methods, and reclassified engine warnings. It also has fatal errors for incompatible method signatures and opaque objects instead of resources for Curl, Gd, Sockets, OpenSSL, XMLWriter, and XML extensions.
Other syntax updates
PHP 8.0 also offers many syntax improvements. For instance, developers can now use “throw” as an expression instead of a statement, making it possible to throw exceptions in different places.
Moreover, PHP 8.0 also has the variable syntax RFC, which helps resolve multiple inconsistencies in the program’s variable syntax. Through this RFC, developers can now handle overlooked small cases.
Furthermore, PHP 8.0 also provides non-capturing catches. Before the update, developers would store an exception in a variable if they wanted to catch it. With non-capturing catches, they can omit the variable.
Additionally, developers can now treat namespaced names as a single token and allow “::class” on objects. Plus, it lets trailing commas in parameter lists, and closure use lists RFC.
How the New PHP Affects Your WordPress Site
Currently, WordPress supports PHP versions 7.4, 8.0, and 8.1. Unfortunately, some users are reluctant to upgrade because the new versions are incompatible with their favorite themes and plugins.
Meanwhile, compatibility issues would only arise if they’re not using the latest WordPress versions, like 5.9 and 6.0. Some prefer not to do so because they prefer working with legacy or outdated software, which prevents them from upgrading to the latest PHP version. Because of this, they tend to encounter more issues in the development process.
On the other hand, developers who’ve tried the latest version of PHP on their WordPress sites experience better and easier development thanks to its new features and improvements. PHP 8.0 and 8.1 offer excellent error handling and optimizations. Plus, the 8.1 plans to have active support and security updates in the following years.
Preparing Your WordPress Site for the PHP Update
As mentioned, PHP 8.0 has specific compatibility issues with older WordPress versions. For a seamless development experience, it’s best to update your site to version 5.9 or 6.0 to support the new PHP functionalities.
If you’re reluctant to upgrade your WordPress site to its latest version, you can test it first with various tools online to see if you can proceed with no errors. Furthermore, before configuring your site for the new PHP 8.0, it’s best to clone it first to avoid data loss due to errors. Once you do, test it on the latest PHP version to see if they’re compatible.
If specific themes and plugins aren’t working with PHP 8.0, you must update them to their latest versions. Instead of returning to the old PHP, contact the theme or plugin’s support team and inform them of the compatibility issue. They should update your site’s features so they can seamlessly work with PHP 8.0 and PHP 8.1.
Empower Your WordPress Site
With modern technology rapidly progressing, the demand for more competitive and high-functioning sites continues to grow. That means being reluctant to adapt to new trends could put your sites behind.
Today, the programming language PHP offers new features that help developers code better than before, thanks to their versions 8.0 and 8.1. While today’s new features already bring so much for developers, PHP promises more for tomorrow’s websites.