PHP 8.4 What's updated and new

-sidebar-toc> -language-notice>
As we add new features, improvements as well as deprecations We anticipate that at this time that 2024 will witness changes to PHP's release cycle, including the closing of security release that is compatible with all versions of PHP currently and were synced to the end of the year in lieu of being released on the GA birthday.
Furthermore, support was extended for one year. That means that you'll be using PHP 8.4 in a safe manner until 2028 (with two years of security, bug fixes and two years of only security updates).
If you're willing to invest more time using PHP 8.4 however, you'll need to find out what's new in this version right now. We'll get right into it.
Improved features and enhancements are included in PHP 8.4
Hooks for property
To illustrate the way property hooks can be used to be used to replace the class below, which contains the properties that are the size
and the flavor
. The properties are protected by private
view to guard the properties from access to anyone who is not part of the object. This is due to the fact that these techniques can be accessed by people in general, facilitate access to the properties
class Coffeeend of class. You can make the coffee you would like to consume. Coffee('Small' "Pumpkin Spice') Print getSize of $coffee() . ' ' . $coffee->getFlavor(); // Prints "Small Pumpkin Spice" // Change order $coffee->setSize('Grande'); $coffee->setFlavor('Mocha'); print $coffee->getSize() . ' ' . $coffee->getFlavor(); // Prints "Grande Mocha"
Maybe your class has many property types, and instead of writing various methods for getting and setting these types, you use PHP's _get
and set
magical methods. You might be able to figure things out using a messy switch
declarations like that below.
// __set magic method example public function __set(string $key, $value): void switch ($key) case 'size': $this->size = $value; break; case 'flavor': $this->flavor = $value; break; default: throw new InvalidArgumentException('Invalid input'); } // Later, we can change the coffee order like this: $coffee->size = 'Grande'; $coffee->flavor = 'Mocha';
What method you select to employ, the greater number of properties you'll have within your class, how far the code that you employ to alter the properties will be with respect to their definitions at the uppermost part of the class file. Additionally, certain methods that implement the "_get"
and set
magic techniques can suddenly allow access to secure or private properties inside your class that you never thought of exposing.
The property hooks are brand new and have allow setting and getter functions along with the property's properties. In the property hooks examples below, you'll find that the properties size
and the $flavor
properties in the Coffee class are now public. Also, we've added some basic checks for set hooks. sets of
hooks to distinguish these from assignments directly.
// Property definitions at the top of our Coffee class class Coffee public string $flavor set(string $value) if (strlen($value) > 16) throw new InvalidArgumentException('Input is too long'); $this->flavor = $value; public string $size set(string $value) if (! in_array($value, array('Small', 'Grande'))) throw new InvalidArgumentException('Not a valid size'); $this->size = $value; // Rest of the Coffee class // Define a coffee $coffee = new Coffee(); $coffee->size = 'Grande'; $coffee->flavor = 'Pumpkin spice';
Like what you see in the following, an get
hook is able to combine functions in what appears to be common reference to a property in an object.
/Simplified Coffee class classes Coffee// Add a flavor $coffee = new () and $coffee->flavor = new() the $coffee->flavor variable will be "Pumpkin". stores the value "Pumpkin" print the"Pumpkin Spice" flavor in the $coffee->flavor variable. prints "Pumpkin Spice"
Contrary with PHP magic tricks Property hooks can be employed in interfaces as well as abstract classes. An interface example:
interface Coffee: get; set;
Asymmetric visibility
Getter and Setter which are publically visible methods that we looked at earlier are the most common method for accessing private or protected properties of their respective classes.
An interesting feature in the PHP 8.4 is the possibility of properties having various levels of visibility based on the context in which it is used. Thus, a property could be visible when read but private when it is secured with a changing the security.
Take a look:
class coffee class: Coffee private(set) flavor is "Pumpkin Spice The $coffee is a coffee with a different flavor() printing the $coffee->flavor. Prints "Pumpkin Spice" The flavor of the $coffee->flavor's flavor is 'Mocha'. The error message (visibility)
In the previous example, it is clear that the class's taste
property is only available within a context of setting. It's pretty simple already and asymmetric visibility provides an opportunity to escape:
class Coffee // public is presumed when the context is not changing private(set) the string $flavor is 'Pumpkin Spice';
Property hooks and asymmetric visibility for creating a massive range of possibilities when working with property of an object with various visibility.
Chaining is a fresh idea
with no parentheses
In the case of shorthands There are the brand new
chains and techniques of chaining that require placing the invocation inside parentheses like this:
$coffee = (new Coffee())->getFlavor()->getSize();
PHP 8.4 allows this:
$coffee = new Coffee()->getFlavor()->getSize();
The code may appear as small, but the removal of just two parentheses makes the code easy to understand and troubleshoot.
The new functions allow you to search the variety of products
In the "You believe we shouldn't be able to have this?" section, PHP 8.4 introduces the function array_find()
, which searches the array for members that meet the criteria that are specified in the callback. The function returns the value of the first element that meets the callback's test.
The new release includes additional functions that are related to it:
array_find_key()
: Likearray_find()
, but the returned value represents the key for the matching element rather than the value of the element.array_all()
: Returnstrue
if every component of the array that is being studied is consistent with the test that the callback is performing.array_any()
: Returnsthe value
in the event that at least one element in the array is in line with the criteria of the callback.
It's crucial to know that the two functions will give the boolean kind of indicators, not array keys, or even content.
Here are a few examples:
$array = [ 'a' => 'Mocha', 'b' => 'Caramel', 'c' => 'Maple', 'd' => 'Pumpkin' ]; // Find the first flavor name that is 5 characters long var_dump(array_find($array, function (string $value) return strlen($value) == 5; )); // Returns "Mocha," even though "Maple" is the same length // Find the array key for the first flavor with a name longer than 5 characters. var_dump(array_find_key($array, function (string $value) return strlen($value) > 5; )); // Returns "b" // Check to see if any flavor name is less than 5 characters long var_dump(array_any($array, function (string $value) return strlen($value)
HTML5 parsing
HTM5 is the industry standard for structuring modern web pages. The Document Object Model (DOM) technology to parse HTML had been discontinued at HTML 4.01.
Instead of inventing a new DOMDocument
class to work in conjunction with the previous HTML standards, PHP 8.4 comes with an entirely new DomHTMLDocument
class that's HTM5-ready.
It is possible to import content of an HTML5 website in the following manner:
$document = DomHTMLDocument::createFromString($html)
In addition to the createFromString($html)
constructor above, the class also supports createFromFile($path)
and createEmpty()
The parser is now able to recognize semantic HTML5 tags like the primary
, article
section and section
that are well-known to the majority of us.
Multibyte trim features
Another feature of PHP 8.4 that appears to have been a while coming is multibyte support in trim functions.
mb_trim()
mb_ltrim()
mb_rtrim()
Similar to the long-running PHP trim()
function, the mb_trim function
removes white space as well as some specific characters, including line feeds, at both ends of a string, which could include multibyte characters. Other functions can trim both sides of the string.
Precautions in PHP 8.4
Each version of PHP contains a variety of brand new functions and features (some rather obscure) that have been flagged as potential removals of this PHP platform. One of the most notable changes to be found in PHP 8.4 is the non-cookie session tracking.
Deprecation of the GET/POST session
In PHP 8.4 the use of either these settings will trigger a warning about deprecation that may appear in your website's logs. Once PHP 9 is released, the settings won't be in use.
Other modifications (and removals) in PHP 8.4
This is a comprehensive list of functionality to be removed by the PHP developers who are who are responsible for PHP 8.4. (Some have links to additional information about the features.,
- Deprecate in a formal way soft-deprecated
properties of DOMDocument
andproperties of DOMEntity.
properties. - Removed
DOMImplementation::getFeature($feature, $version)
. - Deprecate
DOM_PHP_ERR
constant. - Deprecate The "S" tag inside
our informalize()
. - Deprecate
session.sid_length
andsession.sid_bits_per_character
. - Deprecate
SplFixedArray::__wakeup()
. - Eliminate
the set_object xml()
andthe set_*_handler()
with string method names. - It's not recommended to use null or false in
the key_split function dba_key()
. - Do not pass incorrect data types using the function ext/hash for several alternatives.
- Deprecate constants
SUNFUNCS_RET_STRING
,SUNFUNCS_RET_DOUBLE
,SUNFUNCS_RET_TIMESTAMP
. - Avoid using the proprietary CSV escape mechanism.
- Deprecate
E_STRICT
constant. - Deprecate
strtok()
. - Refrain from returning values that are not strings to an output handler for the benefit of.
- It's not advised to produce output within a user-specific output handler.
- Eliminate
file_put_contents()
withthe value $data
in an array. - Remove
mysqli_ping()
andmysqli::ping()
- Deprecate
mysqli_refresh()
. - Deprecate
mysqli_kill()
. - Deprecate the second parameter to
mysqli_store_result()
. - Deprecate
lcg_value()
. - Deprecate
the word uniqid()
. - Get rid of the md5() and sha1() as well as md5_file() and sha1_file().
- It is not recommended to use
E_USER_ERROR
tocause an error()
. - Deprecate using a single underscore ("_") for the class name.
- Deprecate
SOAP_FUNCTIONS_ALL
constant and passing it toSoapServer::addFunction()
.
Summary
Which of the PHP 8.4 features are your top picks? Let us know your thoughts on members of the PHP 8.4 group in the remarks!
Steve Bonisteel
Steve Bonisteel is a Technical Editor for the site. He began his writing career as a newspaper journalist chasing fire trucks and ambulances. The writer has been writing about Internet-related technologies since mid-90s.
Article was posted on here