PHP is so ubiquitous in web development, that anyone involved with websites is usually aware of the release schedule for new versions. The newest version, PHP 8.2 is scheduled for general release this November. In this post, we’ll go over what’s new in PHP 8.2.
New “memory_reset_peak_usage” function
PHP 8.2 adds a new function named “memory_reset_peak_usage” that resets the peak memory usage returned by the memory_get_peak_usage function. This can be helpful in applications that invokes or iterates an action multiple times, and needs to record the peak memory usage of each invocation. Without the ability to reset the memory usage with the new “memory_reset_peak_usage” function, the “memory_get_peak_usage” returns the absolute peak memory usage throughout the entire run.
Readonly Classes
Following the readonly feature in PHP 8.1 for class properties, PHP 8.2 supports declaring an entire class as readonly.
A class declared as readonly automatically makes all properties readonly. PHP 8.2 deprecates dynamic properties, but readonly classes hard-fail dynamic properties with an Error exception. To summarize, a readonly property:
- Only be initialized from within the class scope.
- Cannot be modified once initialized.
- Cannot be unset once initialized.
- Must be a typed property.
MySQLi: New “mysqli_execute_query” function and “mysqli::execute_query” method
In PHP 8.2, the MySQLi extension provides a more straight-forward approach to prepare, bind, execute, and retrieve results from an SQL with a new “mysqli_execute_query” function for procedural API and a new “mysqli::execute_query” method.
The new “mysqli_execute_query” function and “mysqli::execute_query” method accept an SQL query and optionally an array of parameters. The query will be prepared, bound (if parameters are passed), executed within the function/method. It returns a “mysqli_result” object on successful queries, or false if the query was unsuccessful.
True Type
PHP 8.2 allows using true as a type as a standalone type and as part of a Union Type.
Since PHP 8.2, true can be used as a type anywhere PHP accepts a type.
When a parameter, property, or a return type is declared as true, PHP does not coerce any other type to true. This is the case even if PHP script has strict types (strict_types) disabled, and even on values that are otherwise coerced as true in non-strict comparisons.
Upgrading to a recent version of PHP can make a dramatic difference in your website’s performance. At CanSpace, we plan to support PHP 8.2 as soon as it’s released.
If you have any specific questions about how these new functionalities may affect your site, contact us to discuss!