PHP 8.2 programming language release

After a year of development, the release of the PHP 8.2 programming language is presented. The new branch includes a series of new features as well as several interoperability changes.

Key improvements in PHP 8.2:

  • Added the ability to mark a class as read-only. Properties in such classes can be set only once, after which they will not be available for change. Previously, individual class properties could be marked read-only, but now you can turn on this mode for all class properties at once. Specifying the "readonly" flag at the class level also blocks the dynamic addition of properties to the class. readonly class Post { public function __construct( public string $title, public Author $author, ) {} } $post = new Post(/* … */); $post->unknown = 'wrong'; // Error: Cannot create dynamic property Post::$unknown
  • Separate types "true", "false" and "null" have been added, which can take only one valid value and are used, for example, to return a function with an error termination indicator or an empty value. Previously, "true", "false", and "null" could only be used in conjunction with other types (for example, "string|false"), but now they can be used separately: function alwaysFalse(): false { return false; }
  • Provided the ability to filter sensitive settings in the stack trace output at the time of an error. Cutting out certain information may be required when information about errors that occur is automatically sent to third-party services that track problems and inform developers about them. For example, you can exclude parameters from the trace that include usernames, passwords, and environment variables. function test( $foo, #[\SensitiveParameter] $password, $baz ) { throw new Exception('Error'); } test('foo', 'password', 'baz'); Fatal error: Uncaught Exception: Error in test.php:8 Stack trace: #0 test.php(11): test('foo', Object(SensitiveParameterValue), 'baz') #1 {main} thrown in test.php online 8
  • The definition of constants in traits is allowed (trait, a mechanism for code reuse). The constants defined in a trait can be accessed through the class that uses the trait (but not through the trait name). trait Foo { public const CONSTANT = 1; public function bar(): int { return self::CONSTANT; // Fatal error } } class Bar { use Foo; } var_dump(Bar::CONSTANT); // 1
  • Added the ability to specify types in a disjunctive normal form (DNF, Disjunctive Normal Form), which allows you to combine the union of types (collections of two or more types) and the intersection of types (types whose values ​​fall simultaneously under several types). class Foo { public function bar((A&B)|null $entity) { if ($entity === null) { return null; } return $entity; } }
  • A new extension "Random" with functions and classes for generating pseudo-random numbers and sequences is proposed. The module provides an object-oriented interface, allows you to choose different engines for generating pseudo-random numbers, including those suitable for use in cryptography, and provides auxiliary functions, for example, for random mixing of arrays and strings, choosing random array keys, simultaneous use of several generators with its own independent state. $rng = $is_production ? new Random\Engine\Secure() : new Random\Engine\Mt19937(1234); $randomizer = new Random\Randomizer($rng); $randomizer->shuffleString('foobar');
  • Implemented locale-independent case conversion. Functions like strtolower() and strtoupper() now always convert the case of characters in the ASCII range, as when setting the locale to "C".
  • New functions added: mysqli_execute_query, curl_upkeep, memory_reset_peak_usage, ini_parse_quantity, libxml_get_external_entity_loader, sodium_crypto_stream_xchacha20_xor_ic, openssl_cipher_key_length.
  • New methods added: mysqli::execute_query, ZipArchive::getStreamIndex, ZipArchive::getStreamName, ZipArchive::clearError, ReflectionFunction::isAnonymous, ReflectionMethod::hasPrototype.
  • The ability to dynamically create properties in a class has been deprecated. In PHP 9.0, accessing properties not originally defined in the class will result in an error (ErrorException). Classes that provide __get and __set methods for creating properties, or dynamic properties in stdClass will continue to work unchanged, only implicit work with non-existent properties will be stopped in order to protect the developer from hidden errors. To keep the old code working, the "#[AllowDynamicProperties]" attribute is proposed, allowing the use of dynamic properties.
  • The ability to substitute variable values ​​into strings using "${var}" and ${(var)}" expressions has been deprecated. Support for commonly used "{$var}" and "$var" substitutions has been retained. For example: "Hello {$world}"; OK "Hello $world"; OK "Hello ${world}"; Deprecated: Using ${} in strings is deprecated
  • Partially supported callables that can be called via "call_user_func($callable)" have been deprecated, but do not support calling in the form "$callable()": "self::method" "parent::method" "static ::method" ["self", "method"] ["parent", "method"] ["static", "method"] ["Foo", "Bar::method"] [new Foo, "Bar: :method"]
  • The error_log_mode directive has been added to the settings, which allows you to define the access mode to the log with errors.

Source: opennet.ru

Add a comment