search
Softwium

Code. Bug. Fix. Iterate.

PHP 8.3 has been released – explore the language’s new features

The stable version of PHP 8.3 was released on November 23rd. We take a look at the new features and changes it brings, as well as the deprecated elements.

As a reminder, it is customary for the PHP project to release a major and minor versions of PHP on the fourth Thursday of November  each year.

This new language release doesn't introduce significant additions, unlike what we've seen in previous versions (PHP 8.1 and 8.2).

json_validate()

This new function called json_validate() validates if a string contains valid json. Instead of using json_decode() to validate a JSON string, you can now use json_validate().

json_validate('{ "test": { "foo": "bar" } }');

json_validate() takes less resources than json_decode(), so, it is very welcome.

Learn more: json_validate()

Evolution of Constants: Dynamic class constant & Typing

PHP employs various methods for looking up member names, but this wasn't the case for class constants, it is now possible. And constants can now be typed!

class Foo {
    const string BAR = 'bar';
}
$bar = 'BAR';
 
// This is currently a syntax error
echo Foo::{$bar}; 

Randomizer additions

The Randomizer class introduced in PHP 8.2 now benefits from three new methods with PHP 8.3:

  • getBytesFromString(): allows randomly selecting x characters from a given string (where x is provided as input).
  • getFloat(): generates a random float between two given numbers (minimum and maximum).
  • nextFloat(): similar to getFloat, but always returns a float between 0 and 1 (this function does not require any parameters).

Override Attribute

A new attribute #[\Override] indicates that a method will override the parent method.

When this attribute is present, the PHP engine will verify if the specified method indeed exists in the parent class and generate an error if it does not.

The addition of the attribute clearly indicates that the override of a parent method is intentional and streamlines restructuring, as the removal of an overridden parent method will be detected.

class P {
    protected function p(): void {}
}
 
class C extends P {
    #[\Override]
    public function p(): void {}
}

The command-line linter supports multiple files

It is now possible to pass multiple PHP files and PHP CLI lints all of them in the same invocation:

php -l foo.php bar.php
No syntax errors detected in foo.php
No syntax errors detected in bar.php

Well, now all that's left is to wait for PHP 8.4.




Leave a Reply