It’s time to start using PHP 8.4 now

Ingmar Aasoja
RSS: Share:

PHP version 8.4 was released at the end of November. Zone customers have been able to test the new generation of PHP with earlier releases, from beta to release candidate versions. Now, the final version is available on our servers. Here’s a quick overview of the updates and features that came with the latest version.

It's time to start using PHP 8.4 now

Initialising an object without brackets

Although this is a rather cosmetic update for code writers, once initialised, using an object becomes easier. There is no need to put the class name of the new object in the brackets anymore.

PHP 8.3

(new Class())->someFunction();Code language: PHP (php)

PHP 8.4

new Class()->someFunction();Code language: PHP (php)

Property hooks

A syntax known from many programming languages that helps writers describe classes a little more cleanly and, in some cases, reduce using the getters and setters that seemed redundant.

PHP 8.3

class Person 
{
    private string $name;
    
    public function setName(string $name) 
    {
        if (is_numeric($name)) {
            throw new Exception('Invalid name');
        }
        $this->name = $name;
    }

    public function getName(): string
    {
        return $this->name;
    }
}

$person = new Person();
$person->setName('John');
echo $person->getName(); // 'John'
$person->setName('123'); // errorCode language: PHP (php)

PHP 8.4

class Person 
{
    public string $name
    {
        set($name) {
            if (is_numeric($name)) {
                throw new Exception('Invalid name');
            }
        }
    }
}

$person = new Person();
$person->name = 'John';
echo $person->name; // 'John'
$person->name = '123'; // errorCode language: PHP (php)

Asymmetric visibility

Similar to the above, asymmetric visibility helps to clean the code of redundant checks and the resulting getters and setters.

PHP 8.3

class Something 
{
    private bool $isModified = false;

    public function modify()
    {
         // ...something
         $this->isModified = true;
    }

    public function getIsModified(): bool
    {
        return $this->isModified;
    }
}

$foo = new Something();
$foo->modify();
var_dump($foo->getIsModified()); // true
$foo->isModified = false; // error;Code language: PHP (php)

PHP 8.4

class Something 
{
    public private(set) bool $isModified = false;

    public function modify()
    {
         // ...something
         $this->isModified = true;
    }
}

$foo = new Something();
$foo->modify();
var_dump($foo->isModified); // true
$foo->isModified = false; // error;Code language: PHP (php)

Deprecated attribute

An option has been added to set a function or method as deprecated so that it is detected in a similar way to PHP’s internal deprecations, by adding a Deprecated attribute to the corresponding function or method.

PHP 8.3

class Person
{
    /**
     * @deprecated 1.9 use getName()
     */
    public function getPersonName()
    {
        return $this->getName();
    }
    
    public function getName()
    {
        return 'Pirgit';
    }
}

(new Person())->getPersonName(); // no warningCode language: PHP (php)

PHP 8.4

class Person
{
    #[\Deprecated(
        message: 'use getName()',
        since: '1.9',
    )]
    public function getPersonName()
    {
        return $this->getName();
    }
    
    public function getName()
    {
        return 'Pirgit';
    }
}

(new Person())->getPersonName(); // Deprecated: Method Person::getPersonName() is deprecated since 1.9, use getName()Code language: PHP (php)

ext-dom development and HTML5 support

New DOM API adds support for parsing standards-compliant HTML5 documents. It also fixes a number of bugs in the background that affected DOM functionality. To avoid breaking anything down retroactively, the new API uses the Dom namespace. The corresponding objects can be initialised with the classes Dom\HTMLDocument and Dom\XMLDocument. On a technical note, it is based on a new library named Lexbor instead of the previous libxml. If you are more interested in such changes, there is a more in-depth blog post available

BCMath Object API

When writing code that deals a lot with numbers (for example, in a currency conversion application), the new PHP version introduces the possibility of handling numbers as objects.

PHP 8.3

$number1 = '1.5544';
$number2 = '1.00';
$result = bcadd($number1, $number2, 4);
echo $result; // '2.5544'Code language: PHP (php)

PHP 8.4

use BcMath\Number;
$number1 = new Number('1.5544');
$number2 = new Number('1.00');
$result = $number1 + $number2;
echo $result; // '2.5544'
var_dump($number1 < $number2); // falseCode language: PHP (php)

New array_* functions

Until now, handling arrays without installing a framework or an additional package required a lot of extra work in some cases. Now, PHP provides new built-in functions for this.

array_find()
array_find_key()
array_any()
and array_all()

We recommend that you start using PHP 8.4 as soon as possible

In addition to the above, many other features have been added that we haven’t mentioned here. You can read about them on the PHP website. As usual, this version also includes some functionality that has been deprecated, so you might want to make sure they are not in use on your site before upgrading.

We encourage you to check out if your website already works with the new version of PHP and if possible, start using it. If you would like us to update your version of PHP for you in the future, we recommend setting up a PHP Upgrade Strategy on your website via My Zone.

Due to the new version, we are reviewing the PHP versions in the strategies. Soon PHP will be upgraded to version 8.3 for those who use the “Zone Recommended“ strategy. “WordPress“ strategy users will continue to use PHP 8.2, as support for version 8.3 is still partly in beta status according to WordPress developers. We will inform customers in advance by e-mail of any version changes depending on the strategy. 

Popular posts

Why choose a .EU domain today?

Why choose a .EU domain today?

Jaanus Putting
We live in a time where global power dynamics are shifting faster than ever before. While Europe is moving towards a stronger, more unified internal market,...
Ecommerce SEO essentials: How to boost search visibility and drive sales

Ecommerce SEO essentials: How to boost search visibility and drive sales

Montonio
When someone Googles phrases like “best budget trail shoes” or “ceramic non-stick skillet,” they’re not browsing casually — they’re ready...
New at Zone: Varist – even stronger malware protection

New at Zone: Varist – even stronger malware protection

Ardi Jürgens
Email remains one of the easiest ways for cybercriminals to get into businesses. But there’s good news for users of our Premium Email services: since...
Web hosting for beginners: build your website quickly and securely with Zone

Web hosting for beginners: build your website quickly and securely with Zone

Nikita Tikhomirov
Creating your own website might seem daunting, especially if you’ve never dealt with servers, domains, or web design before. Zone makes the process simple...