Update 8.4 has finally arrived for PHP. This update includes massive improvements and new features that boost the already highly-regarded language used by CMS, web pages, and e-commerce sites. Possibly the most notable PHP-written platform is WordPress (what we build 99% of our websites one), which is seeing a lot of improvements across all of its sites and plugins. PHP isn’t the only language used for WordPress, but it is the most significant.

While this new update does include a lot of new features for users to enjoy, it also includes many fixes, updates, and “general cleanup”.

Let’s dig into the details and see what’s changed.

Property Hooks

Property hooks are a way of modifying or overriding the behavior of a certain property. Instead of having to use getters and setters to modify a property, they’ll be stripped from the API so the property hooks can specifically work on behavior.

The property hooks inclusion allows for the introduction of virtual properties, and references to property values, and can even improve class refactoring.

Example

class User {
    public string $name;
    function __get($prop) { return $this->$prop ?? 'N/A'; }
    function __set($prop, $val) { $this->$prop = $val; }
}
$user = new User(); $user->name = 'Alice'; echo $user->name;

Asymmetric Visibility

By allowing a property to have different visibility levels, asymmetric visibility creates different visibility levels for various reading and writing operations.

With update 8.4, both reading and writing sets now have separate visibility and different values.

Example


class Product {
    public function __construct(
        public readonly int $id,
        private string $name
    ) {}
}
$product = new Product(1, 'Book'); echo $product->id;
                        

New Array Functions

Data within an array will often need some form of manipulation, and that’s where array functions come in. These are the 4 major array functions that were introduced in 8.4:

array_find()

Returns the value of the array’s first element for the chosen callback.

array_find_key()

Returns the key of the array’s first element for the chosen callback.

array_any()

It will return true if the callback is true for any given element.

array_all()

If the callback returns true for all elements, the function returns true, and if it doesn’t it returns false.

Example


$nums = [1, 2, 3];
echo array_find($nums, fn($n) => $n > 1);
                        

Simplified Object Instantiation

Simply put programmers no longer need to use parentheses when chaining methods in PHP, as objects can now be created and called on without them.

Example


class Logger { function log() { echo 'Logged'; } }
(new Logger)->log();
                        

Enhanced HTML5 Parsing with \Dom\HTMLDocument

Whenever an HTML document is uploaded into a web browser, it becomes an object. With this compliant parser, programmers can now more easily extract a document’s information with a tool that’s also backwards compatible.

So now, PHP can also interpret documents from older versions of HTML.

Example


$html = '<p>Hello</p>';
$doc = new \Dom\HTMLDocument($html);
echo $doc->textContent;
                        

#[\Deprecated] Attribute

The attribute gives devs and programmers a chance to update or improve their code by marking different functions, class constants, or methods as deprecated. When it’s utilized PHP will then automatically generate a deprecated message on the relevant function, method, or constant.

Example


#[Deprecated("Use newFunc instead", "8.4")]
function oldFunc() { echo 'Old'; }
                        

Object API for BCMath

With object-oriented methods, users can now interact with the BCMath library, which can create a more intuitive syntax for various calculations that are performed directly on the given object instead of the previous procedural functions.

Example


use BcMath\Number;
$a = new Number('2'); $b = new Number('3');
echo $a + $b;
                        

PDO Driver-Specific Subclasses

These new subclasses extend access to the PDO (PHP Data Objects) class, which grants access to different features that are unique to their respective database systems. That includes MySQL, SQLite, and PostgreSQL.

Now, developers have more methods from the specific database driver that they’re currently using.

Example


$pdo = new \Pdo\MySql('mysql:host=localhost;dbname=test', 'root', '');
                        

New Rounding Modes

Now, integers have more specific “rounding behaviors” that are more complex than just rounding to the closest other integer. They can now round up to the nearest integer (PHP_ROUNDING_CEILING), round down to the nearest integer (PHP_ROUND_FLOOR), round the integer away from zero (PHP_ROUND_TOWARD_ZERO), and round away from zero (PHP_ROUND_AWAY_FROM_ZERO).

Example


echo round(2.5, 0, PHP_ROUND_HALF_UP)
                        

Deprecation of Implicit Nullable Types

Deprecation of implicit nullable types is no longer recommended, or even supported in the new version of PHP, now they need to be explicitly declared as nullable.

This can be done using a simple syntax function whenever certain values need to be nulled.

Example


function greet(?string $name) { echo $name ?? 'Guest'; }
greet(null);