Skip to content
Exploring PHP 8.4: Exciting New Features with Examples

Exploring PHP 8.4: Exciting New Features with Examples

Published on
by Raziul Islam

The upcoming PHP 8.4 release promises to bring several new features and improvements that simplify development and improve performance. As a PHP developer, exploring these changes and understanding how they can impact your projects is always exciting.

In this article, we’ll walk you through some of the most exciting additions to PHP 8.4 with examples demonstrating how they enhance readability, security, and functionality.

1. New Class Instantiation Syntax: Cleaner and Simpler

PHP 8.4 introduces a tweak that eliminates the need for parentheses when instantiating a class without constructor arguments. This update might seem minor, but it simplifies code, making it cleaner and easier to read.

// Before
$dog = (new Dog())->bark();
// After PHP 8.4
$dog = new Dog()->bark();

Notice how removing the parentheses around new Dog() reduces visual clutter. While this doesn’t add functionality, it improves the developer experience, especially in object-oriented code where such instantiations are common.

Why This Change Matters

Although the functional difference is minimal, every bit of syntactic sugar helps make the code more concise. This tweak improves code readability, especially in larger applications where object instantiation is frequent.


2. New Array Helper Functions: Efficiently Manage Arrays

PHP 8.4 introduces several new array helper functions that simplify common tasks like searching, checking, and filtering arrays. These functions are inspired by common functional programming patterns, making your code easier to write and more readable.

array_find

The array_find() function allows you to find the first element in an array that matches a specified condition. It’s a concise alternative to array_filter() when you only need the first matching element.

Example:

$array = ['dog', 'cat', 'elephant', 'goose'];
// Find the first animal whose name is longer than 4 characters
$result = array_find($array, fn($value) => strlen($value) > 4);
var_dump($result); // Outputs: string(8) "elephant"

If no matching element is found, array_find() returns null. This function is a neat alternative to writing a loop or using array_filter().

array_find_key

Similar to array_find(), this function returns the key of the first matching element instead of the value.

Example:

$array = ['a' => 'dog', 'b' => 'cat', 'c' => 'elephant'];
// Find the key of the first animal name longer than 4 characters
$result = array_find_key($array, fn($value) => strlen($value) > 4);
var_dump($result); // Outputs: string(1) "c"

array_any

This function checks whether any element in an array matches a given condition. It returns true if at least one match is found, and false otherwise.

Example:

$array = ['dog', 'cat', 'elephant'];
// Check if any animal name has more than 7 characters
$result = array_any($array, fn($value) => strlen($value) > 7);
var_dump($result); // Outputs: bool(true)

array_all

On the flip side, array_all() checks whether all elements match a given condition.

Example:

$array = ['dog', 'cat', 'goose'];
// Check if all animal names are less than 10 characters long
$result = array_all($array, fn($value) => strlen($value) < 10);
var_dump($result); // Outputs: bool(true)

Why These Functions Are a Game-Changer

These functions reduce boilerplate code, especially when dealing with complex conditions and filters. Instead of writing loops or using array manipulation techniques, you can now achieve the same with a few lines of code.


3. DOM Extension with HTML5 Support

The DOM extension in PHP 8.4 gets a significant upgrade: full HTML5 parsing and serialization support. This means the DOM extension can now handle modern HTML5 tags and structures, ensuring compatibility with today’s web standards.

Example:

use DOM\HTMLDocument;
// Create an HTML5 document from a string
$html = HTMLDocument::createFromString('<!DOCTYPE html><html><body>Hello, HTML5!</body></html>');

This feature is especially useful when working with modern web applications that incorporate HTML5 elements. It removes the need for workarounds when parsing and serializing HTML5 documents.


4. Increased Bcrypt Cost for Better Security

In PHP 8.4, the default bcrypt cost has been increased from 10 to 12. Bcrypt is a password hashing algorithm, and the “cost” determines how computationally intensive the hashing process is. A higher cost makes the hashing process slower, which in turn increases the time required for brute-force attacks.

Example:

// Using password_hash() with default bcrypt cost of 12
$hash = password_hash('my_secret_password', PASSWORD_BCRYPT);
echo $hash;

This update ensures stronger password protection by default, making it harder for attackers to crack passwords with brute-force attacks.


5. Improved XML Parsing for Large Documents

PHP 8.4 introduces a fix for handling large XML documents, addressing an issue in earlier versions where parsing large files could fail due to limitations in the ext/xml extension. This change is especially helpful for applications that need to handle big datasets, such as APIs or XML-based content management systems.

Example:

$parser = xml_parser_create();
// New option to handle huge XML files
xml_parser_set_option($parser, XML_OPTION_PARSE_HUGE, 1);
// Parse the large XML file
xml_parse($parser, file_get_contents('large.xml'));

With this update, large XML documents can now be parsed reliably, removing a previous pain point for developers.


6. Multibyte Trimming Functions

PHP 8.4 adds new multibyte-safe string trimming functions, making it easier to handle strings with multibyte characters. These functions—mb_trim(), mb_ltrim(), and mb_rtrim()—improve upon the previous method of using regular expressions for trimming.

Example:

$string = " あいうえお ";
$trimmed = mb_trim($string);
var_dump($trimmed); // Outputs: "あいうえお"

These functions ensure that multibyte characters are handled correctly, making internationalization and working with non-ASCII characters easier.


Conclusion

PHP 8.4 introduces several exciting features aimed at making development easier and more secure. Whether it’s the cleaner class instantiation syntax, powerful new array helper functions, improved DOM extension support for HTML5, or enhanced bcrypt hashing, these changes reflect PHP’s ongoing evolution as a modern programming language.

These improvements reduce repetitive code, improve security, and make working with modern web technologies simpler. With these changes, PHP 8.4 is set to become an even more developer-friendly language, keeping up with contemporary development standards.

Raziul Islam

Raziul Islam

Creating future-proof web solutions with cutting-edge technologies like Laravel, Livewire, and JavaScript.

Posts you might like