After a year of development, the release of the PHP 8.3 programming language is presented. The new branch includes a series of new features as well as several interoperability changes.
Key changes in PHP 8.3:
- During class cloning, it is possible to reinitialize properties with the “readonly” attribute. Overriding readonly properties is allowed only inside the “__clone” function: readonly class Post { public function __construct( public DateTime $createdAt, ) {} public function __clone() { $this->createdAt = new DateTime(); // allowed even though the "createdAt" property is read-only. } }
- The ability to use constants with type indication in classes, traits and enumerations has been provided: class Foo { const string BAR = 'baz'; }
- Added support for the “#[Override]” attribute, with which the developer can inform the interpreter that the marked method overrides some parent method. If there is no override, the interpreter will display an error.
- Changed handling of negative values as an array index. For example, when adding an element with number “-5” to an empty array and adding another element, previously the second element was saved with index “0”, but starting from version PHP 8.3 it will be saved with index “-4”. $array = []; $array[-5] = 'a'; $array[] = 'b'; var_export($array); // Was array (-5 => 'a', 0 => 'b') // Became array (-5 => 'a', -4 => 'b')
- Added the ability to create anonymous classes in read-only mode: $class = new readonly class { public function __construct( public string $foo = 'bar', ) {} };
- Added json_validate() function to quickly check if a string is in JSON format without performing decoding operations. json_validate(string $json, int $depth = 512, int $flags = 0): bool
- New methods have been added to the Randomizer class, which provides a high-level API for generating pseudo-random numbers and sequences: getBytesFromString for generating a string of a given size, using in random order the characters present in another string; getFloat and nextFloat to generate a random floating point number that falls within the specified range.
- Added the ability to retrieve constants using dynamic class syntax: class Foo { const BAR = 'bar'; } $name = 'BAR'; // Previously, to retrieve the BAR constant, you had to call constant(Foo::class . '::' . $name); // Now just specify Foo::{$name};
- Added generation of individual exceptions (DateMalformedIntervalStringException, DateInvalidOperationException, DateRangeError) in case of problems arising in operations working with dates and time.
- Improved handling of errors that occur during parsing of serialized data in the unserialize() function. In case of problems, unserialize() now issues E_WARNING instead of E_NOTICE.
- Changes have been made to the range() function. An exception is generated when attempting to pass objects, resources or arrays in variables that define range boundaries, as well as when specifying a negative value in the $step parameter or an undefined value in any parameter. A list of characters can now be output when specifying strings instead of numbers (for example, “range('5', 'z')").
- Changed the behavior of traits with static properties, which now override static properties inherited from the parent class.
- Added settings for stack overflow protection. The zend.max_allowed_stack_size and zend.reserved_stack_size directives have been added to the ini file, defining the maximum allowed and reserved stack size. The program will crash when approaching stack exhaustion, when the stack is full more than the difference between zend.max_allowed_stack_size and zend.reserved_stack_size (execution will stop before a segmentation fault occurs). By default, the zend.max_allowed_stack_size value is set to 0 (0—the size is determined automatically; to disable the limitation, you can set it to -1).
- Added new POSIX functions posix_sysconf(), posix_pathconf(), posix_fpathconf() and posix_eaccess().
- The mb_str_pad function has been added, which is an analogue of the str_pad() string function, designed to work with multi-byte encodings such as UTF-8.
- Allows you to create closures from methods and pass named arguments to those closures. $test = new Test(); $closure = $test->magic(…); $closure(a: 'hello', b: 'world');
- Changed behavior when handling the visibility of constants in interfaces. interface I { public const FOO = 'foo'; } class C implements I { private const FOO = 'foo'; }
- The capabilities of the array_sum(), array_product(), posix_getrlimit(), gc_status(), class_alias(), mysqli_poll(), array_pad() and proc_get_status() functions have been expanded.
- The ability to pass a negative $widths value to mb_strimwidth() has been deprecated. The NumberFormatter::TYPE_CURRENCY constant has been removed. Support for calling the ldap_connect() function with two parameters $host and $port has been discontinued. The opcache.consistency_checks setting has been removed.
Source: opennet.ru
