What’s new in PHP 8.2? (7 features for you)
Coding (PHP 8)
A comprehensive guide to the latest features and improvements
Let's say you developed a web application using PHP 7.0.
Over time, new versions of PHP have been released, each with new features and performance enhancements.
However, you haven't updated your application to use the latest version of PHP.
As a result, your application is still running on PHP 7.0, which may be missing out on important security patches and performance improvements.
If a security vulnerability is discovered in PHP 7.0, your application could be at risk of being exploited by attackers.
Additionally, your application may be running slower than it could be if it were updated to use a more recent version of PHP with better performance optimizations.
Now, let's imagine that you decided to update your application to use PHP 8.0, which was released in November 2020.
By doing so, you would gain access to many new features and performance improvements, such as the JIT compiler, union types, attributes, and more.
Your application would also benefit from any security patches released in the newer version.
By staying up to date with the latest version of PHP, you can ensure that your application is secure, optimized, and take advantage of the latest features and performance improvements.
In this blog post, we'll take a step further and take a look at some of the most notable features and improvements in PHP 8.2.
Improved performance
One of the most significant improvements in PHP 8.2 is its improved performance.
The new release introduces a new JIT (Just-In-Time) compiler that can compile PHP code into machine code at runtime, resulting in faster code execution.
This feature is especially useful for applications with high traffic and intensive CPU usages, such as large-scale web applications and APIs.
You can read more about those improvements on the Official PHP website.
What is JIT?
A JIT (Just-In-Time) compiler is a type of compiler that dynamically compiles code at runtime, instead of ahead of time.
The JIT compiler takes the intermediate representation of the code, typically bytecode, and translates it into machine code that can be executed by the CPU.
JIT compilers are used in a variety of programming languages and environments, including Java, .NET, and now PHP 8.2.
The benefit of using a JIT compiler is that it can improve the performance of the code by compiling it on the fly, taking advantage of runtime information to optimize the generated machine code.
In the case of PHP, the new JIT compiler in version 8.2 can significantly improve the performance of CPU-intensive tasks, such as numerical computations and machine learning algorithms.
By compiling the PHP code to machine code at runtime, the JIT compiler can eliminate some of the overhead associated with interpreting PHP bytecode, leading to faster execution times.
It’s worth noting that not all code benefits from JIT compilation, and the performance gains can vary depending on the specific use case.
However, in general, JIT compilation can be a powerful optimization technique for certain types of applications.
If all of this seems overwhelming and you want to step back a bit visit “The Basics of PHP”.
New named arguments syntax
Another significant change in PHP 8.2 is the introduction of a new named arguments syntax.
This feature allows developers to pass arguments to functions by name, making it easier to write and read function calls.
Named arguments also allow developers to specify only the parameters they need to pass, making the code more concise and readable.
Arguments syntax refers to how arguments (values passed to a function or method) are specified in the code.
PHP supports two main types of argument syntax: positional arguments and named arguments.
Positional arguments are the traditional way of passing arguments to a function.
In this syntax, the arguments are listed in order and separated by commas.
For example, the following function call passes three positional arguments to the function “myFunction”:
myFunction($arg1, $arg2, $arg3);
Named arguments, on the other hand, allow developers to specify the name of the argument along with its value, rather than relying on the order of the arguments.
This can make function calls more readable and less error-prone, especially when dealing with functions that have many parameters.
Named arguments were introduced in PHP 8.0 and have been further improved in PHP 8.1 and 8.2.
Here’s an example of how named arguments can be used in PHP 8.2:
function myFunction($arg1, $arg2, $arg3) { // function body } // positional argument syntax myFunction('value1', 'value2', 'value3'); // named argument syntax myFunction(arg1: 'value1', arg2: 'value2', arg3: 'value3');
// positional argument syntax myFunction('value1', 'value2', 'value3'); // named argument syntax myFunction(arg1: 'value1', arg2: 'value2', arg3: 'value3');
In this example, the function “myFunction” takes three arguments: $arg1, $arg2, and $arg3.
The first function call uses positional argument syntax to pass the values ‘value1’, ‘value2’, and ‘value3’ in that order.
The second function call uses named argument syntax to pass the same values, but with the argument names specified explicitly.
Note that the order of the named arguments does not matter, as long as each argument is named correctly.
Named arguments can also be combined with default values, allowing developers to specify only the parameters they need to pass.
This can make the code more concise and easier to read.
Union types with leading pipe
PHP 8.2 introduces a new syntax for union types, which allows developers to declare multiple types for a single variable or parameter.
The new syntax uses a leading pipe character ("|") to separate the different types.
This change makes it easier to declare and work with complex data structures.
Abel does a wonderful job explaining this concept in his post about union type in PHP
Readonly properties
PHP 8.2 adds a new "readonly" keyword that allows developers to declare class properties as read-only.
Once a property is marked as readonly, it can only be initialized once, either during its declaration or in the constructor.
Readonly properties can help prevent accidental modification of critical data, improving the code's safety and reliability.
Readonly properties are a new feature introduced in PHP 8.1 that allows developers to define properties that can only be initialized once and cannot be modified afterward.
This can help improve the safety and reliability of PHP 8.2 code by preventing accidental or malicious modification of critical data.
To define a readonly property, you use the “readonly” keyword followed by the property name and its initial value, like this:
class MyClass { public readonly string $myProperty = 'initial value'; }
In this example, the “MyClass” class defines a readonly property called “myProperty” of type string.
The property is initialized with the value ‘initial value’, and once set, it cannot be changed.
If you try to modify the value of a readonly property after it has been initialized, PHP will generate an error at runtime.
For example, the following code would generate a fatal error:
$myObject = new MyClass(); $myObject->myProperty = 'new value'; // error: cannot modify readonly property
Readonly properties can be useful in a variety of scenarios, such as when you want to ensure that a value is only set once during object initialization or when you want to prevent modifications to critical configuration values.
By using readonly properties, you can make your code more robust and less prone to bugs and security vulnerabilities.
A more flexible match expression
The "match" expression introduced in PHP 8.0 has been improved in PHP 8.2 to provide more flexibility.
In particular, developers can now use "match" expressions with scalar values, which was not possible before.
I love to use the match expression on new projects as it gives me so much more flexibility.
Additionally, the new release allows developers to use conditional expressions in "match" branches, further enhancing its flexibility and readability.
Would you choose match or switch? this question has been answered by Brent at Stitcher.
Improved type inference
Type inference is a feature in PHP 8.0 and later versions that allows the PHP engine to automatically determine the data type of a variable based on its initial value.
This means you don’t need to specify the data type explicitly when declaring a variable, and PHP 8.2 will infer the data type from the context.
Here’s an example of how type inference works in PHP:
$myVariable = 'hello'; // $myVariable is inferred as a string $myArray = [1, 2, 3]; // $myArray is inferred as an array of integers $myObject = new stdClass(); // $myObject is inferred as an instance of stdClass
PHP 8.2 infers the data type of the variables based on their initial values.
The variable $myVariable is inferred as a string because its initial value is a string literal.
The variable $myArray is inferred as an array of integers because its initial value is an array containing integer values.
The variable $myObject is inferred as an instance of the stdClass class because it’s initialized with the “new” operator followed by the class name.
Need to brush up on what a class is?
Visit The complete guide to Object-Oriented Programming.
Type inference can make your code more concise and easier to read, as you don’t need to specify the data type of each variable explicitly.
However, it’s important to note that type inference is not always possible or desirable in all situations.
In some cases, you may want to explicitly specify the data type of a variable to ensure type safety and avoid bugs.
New function attributes
PHP 8.2 introduces several new function attributes that can be used to annotate functions with additional information.
For example, the new "@unsafe" attribute can be used to indicate that a function may have side effects or cause security issues, while the "@memoize" attribute can be used to indicate that a function's result can be cached
Conclusion
In conclusion,
PHP 8.2 brings several exciting new features and improvements to the language.
From improved performance and syntax changes to enhanced type inference and new function attributes, this release is packed with features that can help developers write better, safer, and more efficient code.
If you're a PHP developer, upgrading to PHP 8.2 is a no-brainer, and we can't wait to see what you'll create with it!
Subscribe to the newsletter and stay ahead of the curve!
Get fresh, informative content delivered straight to your inbox, covering a wide range of topics from the PHP world and beyond.