![](https://www.wpfastworld.com/wp-content/uploads/2022/12/6392863f70093.jpg)
PHP8.2 is an important milestone in the modernization process of the PHP language. In addition to exciting new features and improvements, PHP 8.2 also simplifies the language, removes support for dynamic class attributes, warns when certain non-optimal ini configurations are encountered, and fixes some issues affecting PHP. Legacy PHP behavior for array sorting and certain types of string conversion/encoding operations.
system improvement
PHP8.2 addresses several shortcomings and limitations of the PHP type system, allowing PHP applications to adopt better type safety. Including adding the true type, allowing null and false to be used as independent types, and supporting DNF types (generic parsing).
PHP8.2 supports separate paradigm types, and now it is possible to combine union types and communicative types, which can define precise and expressive parameters, return values, and attributes.
Before php8.2
class Foo {
public function bar(mixed $entity) {
if ((($entity instanceof A) && ($entity instanceof B)) || ($entity === null)) {
return $entity;
}
throw new Exception('Invalid entity');
}
}
Now
class Foo {
public function bar((A&B)|null $entity) {
return $entity;
}
}
Supports true and false as separate types, which can be used to declare bool if it is always the same.
function alwaysReturnsFalse(): false {}
function alwaysReturnsNull(): null {}
function alwaysReturnsTrue(): true {}
Among them, the null type can be used in the joint type declaration in the previous version, and now it can be used independently.
read-only class
PHP8.1 added a readonly attribute declaration. A readonly attribute can only be set once, and PHP prevents any in-scope modification.
PHP8.2 makes further use of the readonly statement, and the class can be declared as readonly. When a class is declared readonly, all its properties are automatically declared readonly. Also, this class cannot use dynamic properties to ensure that all properties are defined.
readonly class User {
public string $username;
public string $uid;
}
All properties are automatically declared as readonly.
New random number extension
Throughout PHP’s history, it has supported a wide variety of random number generators with varying degrees of performance and different use cases, and suitable for security applications. PHP 8.2 goes a step further and refactors all random number-related functions into an extension called random. The new extension will not break any existing use of the interface, so existing rand, mt_rand functions will continue to work without any changes. It also provides an object interface to generate random numbers with a pluggable system, so it is easy to mock random number generators and provide new random number generators, making PHP applications safe and easy to test.
trait constant
In PHP8.2, constants can be declared in traits. Traits cannot be accessed directly, but when a class uses a trait, these constants become constants of the class.
trait Foo
{
public const CONSTANT = 1;
}
class Bar
{
use Foo;
}
var_dump(Bar::CONSTANT); // 1
var_dump(Foo::CONSTANT); // Error
Sensitive parameter support
PHP8.2 adds a new built-in parameter attribute name: #[\SensitiveParameter]. Enables PHP to hide the actual value in stack traces and error messages.
We often define passwords, secret keys, or other sensitive information in parameters or properties. These values are logged when an error occurs in PHP. Displayed to the screen or logged to the log. So that people can get sensitive data through these means.
Like the following example:
function passwordHash(#[\SensitiveParameter] string $password) {
debug_print_backtrace();
}
passwordHash('hunter2');
The printed content is as follows:
array(1) {
[0]=> array(4) {
["file"]=> string(38) "..."
["line"]=> int(9)
["function"]=> string(3) "foo"
["args"]=> array(1) {
// [0]=> string(38) "hunter2" 这一行不会被打印出来
[0]=> object(SensitiveParameterValue)#1 (0) {}
}
}
}
hunter2 will not be printed.
new functions and classes
Parse INI quantity value: ini_parse_quantity
Recognize PHP ini values as bytes.
ini_parse_quantity('256M'); // 268435456
curl to keep alive:curl_upkeep
In PHP 8.2, the curl extension triggers the underlying curl library to run the necessary tasks to keep the curl connection alive. The most common usage is to periodically call curl_upkeep to achieve http persistent connection (keep-alive).
Retrieve password length:openssl_cipher_key_length
In PHP8.2 OpenSSL, there is a function called openssl_cipher_key_length, which can accept the key length required by any supported cipher, which needs to be hardcoded before:
openssl_cipher_key_length("CHACHA20-POLY1305"); // 32
openssl_cipher_key_length("AES-128-GCM"); // 16
openssl_cipher_key_length("AES-256-GCM"); // 32
Reset recorded peak memory usage:memory_reset_peak_usage
This is useful for multiple or iterative calls.
Deprecation in PHP8.2
PHP8.2 also brings a fair amount of deprecations. When syntax, functions, and features are deprecated, PHP issues a deprecation notice, which should not break PHP programs, but will be logged in the error log.
Note: After PHP8.0, the default error reporting behavior of PHP is E_ALL
Dynamic properties are deprecated
One of the most notable deprecations in PHP 8.2 is the deprecation of dynamic properties. If a class attribute is called or assigned without a declaration, the program will exit.
class User {
public int $uid;
}
$user = new User();
$user->name = 'Foo';
This may affect many legacy PHP programs, and the recommended fix is to declare the property on the type.
There are exceptions to this, like stdClass and its subclasses will work normally, the __get and __set magic methods will work normally, or declare #AllowDynamicProperties.
For some other deprecations, you can follow other articles on this site:
“New usage of string variable parsing in PHP8.2” https://phpreturn.com/index/a628de16a2adf8.html
Install and upgrade to PHP8.2
PHP 8.2 is now available for download/installation in all regular sources:
- Windows: compiled binaries are available at windows.php.net
- Ubuntu/Debian: PHP 8.2 available ondrej/phpPPA
- Fedora/RHEL/CentOS/Alma/Rocky: Available in Remi’s source
- Mac OS: PHP 8.2 available via Homebrew install shivammathur/homebrew-php.
- Docker: PHP 8.2 available via 8.2* releases
For more detailed changes, the author will continue to follow up and release. Welcome to pay attention to favorites.
Original title:PHP8.2 released!
Original address:https://phpreturn.com/index/a639285aa925ed.html
Original platform:PHP arsenal
Copyright Notice:This article is original and published by phpreturn.com (PHP Arsenal official website), and all rights belong to phpreturn (PHP Armory). This site allows any form of reprinting/quoting articles, but the source must be indicated at the same time.
#PHP #News Fast Delivery