PHP 8.4: Introduction of property hooks and asymmetric visibility
The latest release of the PHP programming language brings new features such as property hooks, asymmetric visibility and new array search functions.
(Image: Sashkin/Shutterstock.com)
Version 8.4 of the PHP programming language has been released. The release brings a number of significant innovations and optimizations that should further simplify development with the popular programming language. Highlights of the new version include the introduction of property hooks, asymmetric visibility and new array search functions.
PHP 8.4 brings property hooks
With the introduction of property hooks in PHP 8.4, developers can specifically control the behavior of properties in classes. This feature makes it possible to define get and set hooks to execute custom logic when accessing or changing properties. This reduces the need for extensive getter and setter methods. In addition, properties can now be defined in interfaces, which should make it easier to specify type information and operations. Property hooks therefore offer more precise control over the life cycle of properties.
Videos by heise
Asymmetric visibility
Asymmetric property visibility in PHP 8.4 makes it possible to define different visibilities for reading and writing properties. A property can be configured so that it is publicly readable but only internally modifiable. This feature is designed to give developers more flexibility and control over access to properties in classes. An example from the PHP 8.4 announcement post:
<?php
class Example
{
// The first visibility modifier controls the get-visibility, and the second modifier
// controls the set-visibility. The get-visibility must not be narrower than set-visibility.
public protected(set) string $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
New array find functions
PHP 8.4 brings new array find functions to make it easier for developers to find and validate values in arrays. The new functions include:
array_findarray_find_keyarray_anyarray_all
These functions each accept an array and a callback as arguments.
array_find returns the first element for which the callback returns true, while array_find_key returns the key of this element. array_any checks whether at least one element in the array fulfills the callback, and array_all checks whether all elements in the array pass the callback. These innovations aim to create a more precise and efficient way of working with arrays by encapsulating frequently required search and check logic.
Simplifying the syntax
PHP 8.4 comes with revised syntax, allowing developers to omit the parentheses around the instantiation when directly instantiating a class and then calling a method. Previously, they had to use brackets.
The development team shows a comparison of the syntax before PHP 8.4 with the new version:
PHP < 8.4
class PhpVersion
{
public function getVersion(): string
{
return 'PHP 8.3';
}
}
var_dump((new PhpVersion())->getVersion());
PHP 8.4
class PhpVersion
{
public function getVersion(): string
{
return 'PHP 8.4';
}
}
var_dump(new PhpVersion()->getVersion());
PHP 8.4 provides more readability with the simplification of the syntax, especially in situations where a class is instantiated and a method is called immediately without the instance being needed further.
Just in time
The update changes the default values for the INI configuration options for just-in-time compilation (JIT), although JIT remains disabled by default. The changes affect the values for opcache.jit and opcache.jit_buffer_size, which developers can now set to tracing or disable and 0 or 64M – JIT requires an explicit configuration of the buffer size in order to be activated.
Support for HTML
The development team behind PHP 8.4 has overhauled support for HTML5 by introducing a more powerful HTML5 parsing library. Previously, the DOM parser in PHP only supported HTML 4.01 functions. With the update, PHP now offers comprehensive support for HTML5. There are new DOM classes in a separate PHP namespace that have been developed specifically for HTML5 and are different from the existing XML-oriented DOM classes. These changes allow developers to make better use of HTML5 standards when parsing or creating HTML documents.
Other new features
PHP 8.4 introduces support for additional HTTP verbs for $_POST and $_FILES as well as new multi-byte trim functions such as mb_trim(). There are clearer rounding modes for round() and specific PDO subclasses for database drivers. Revised callbacks for DOM and XSL now support closures. Support for lazy objects enables the creation of proxy objects. A new attribute, #[Deprecated], marks deprecated functions, and there are revisions to the BCMath, GMP and DOM extensions. The latter have already been described in the article on the first release candidate for PHP 8.4.
Further information on the release can be found on php.net. The ChangeLog contains additional details on the new features. The developers also provide a migration guide.
(mdo)