This article is part of the CakeDC Advent Calendar 2024 (December 14th 2024)
In this article, we will continue to explore the new features and improvements added in the latest version of PHP 8.4.
In this release, we have received several long-awaited improvements, improved functionality, and a modern approach to common tasks. These few seem to be the most interesting:
- New ext-dom features and HTML5 support
new MyClass()->method()
without parentheses#[\Deprecated]
Attribute
New ext-dom features and HTML5 support
One of the new feature in the latest PHP release is the new DOM API with support for HTML5, which makes it much easier to work with parsing and extracting data from HTML documents.
$html = <<<'HTML'
<main>
<header>
<h1>HTML5 Example Page</h1>
</header>
<nav class="top-nav">
<ul>
<li><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Contact</a></li>
</ul>
</nav>
<article>...</article>
<nav class="bottom-nav">
<ul>
<li><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Contact</a></li>
</ul>
</nav>
</main>
HTML;
$htmlDocument = HTMLDocument::createFromString($html);
$node = $htmlDocument->querySelector('main > nav:first-of-type');
echo $node->className; // prints top-nav
new MyClass()->method()
without parentheses
New expressions can now be used without additional parentheses, making method chaining and property access much easier.
// CakePHP collections can be created using new expression without additional parentheses
new Collection([1, 2, 3])
->filter(fn (int $item): bool => $item !== 2)
->map(fn (int $item): string => $item . "000 pcs")
->each(function (string $item): void {
echo $item;
})
// the same goes for the classes like DateTime or Date
echo new DateTime()->addDays(10)->toIso8601String();
New #[\Deprecated]
Attribute
The new attribute #[\Deprecated]
will allow developers to mark their functions, methods and class constants as
deprecated. Using a statement marked with this attribute will result in the error E_USER_DEPRECATED
being
emitted. This attribute makes it easier to manage deprecated code and uses existing mechanisms in PHP but
in user code.
class MyClass
{
#[\Deprecated(message: "use MyClass::MODERN_CLASS_CONST instead", since: "7.1")]
public const LEGACY_CLASS_CONST = 'Legacy Class Constant';
public const MODERN_CLASS_CONST = 'Modern Class Constant';
public function doSomething(string $argument)
{
...
}
#[\Deprecated(message: "use MyClass::doSomething() instead", since: "7.1")]
public function doSomethingLegacyAndUnsafeWay(string $argument)
{
...
}
}
$obj = new MyClass();
$obj->doSomethingLegacyAndUnsafeWay('foo');
echo $obj::LEGACY_CLASS_CONST;
The above code will emit warnings:
Deprecated: Method MyClass::doSomethingLegacyAndUnsafeWay() is deprecated since 7.1, Use MyClass::doSomething() instead
Deprecated: Constant MyClass::LEGACY_CLASS_CONST is deprecated since 7.1, use MyClass::MODERN_CLASS_CONST instead
Conclusion
The above features greatly extend the capabilities of the PHP language.
Improved HTML5 support, simplified creation of new objects and deprecation management from PHP will greatly facilitate your tasks in these areas.
In addition to these improvements, PHP 8.4 also offers a number of other minor improvements and additions, check the detailed changelog for more information
This article is part of the CakeDC Advent Calendar 2024 (December 14th 2024)