Some interesting changes in PHP 8.3

Iman Borumand Zadeh
3 min readMar 26, 2023

--

PHP 8.3

As we know, PHP 8.3 will be released on November 23, 2023. In this article, I am going to introduce you to some new features and changes that will be in PHP version 8.3. So stay with me (:

json_validate() function

json_validate(string $json, int $depth = 512, int $flags = 0): bool

Here, $json is the JSON string that needs to be validated, $depth is the nesting depth (maximum) of the decoding structure, $flags is the bitcode of decode flags.

As we know, until now there was no function for validating JSON in PHP. And we had to do this for example as follows:

 $json = '{"name": "Iman Borumand Zadeh"}';
$data = json_decode($json); // function to validate JSON
if (json_last_error() === JSON_ERROR_NONE) {
// Valid JSON
} else {
// Invalid JSON
}

But in PHP version 8.3, a new function called json_validate has been added. This function does the job of validating JSON strings easily for us. Just give the JSON input to this function:

$json = '{"name": "Iman Borumand Zadeh"}';
if (json_validate($json)) {
// Valid JSON
} else {
// Invalid JSON
}

As you can see, using the json_validate function is much easier and more convenient. This function receives a string input and returns true if valid and false otherwise.

getFloat() method

The getFloat() method allows you to generate a random float number between two given numbers.

$randomizer = new \Random\Randomizer();

$randomizer->getFloat(0, 1); // 0.163456789

We can use this method to generate random latitude and longitude, too

$randomizer = new \Random\Randomizer();

var_dump(
$randomizer->getFloat(-90, 90, \Random\IntervalBoundary::ClosedClosed),
$randomizer->getFloat(-180, 180, \Random\IntervalBoundary::OpenClosed)
);

// Lat: float(-45.123456789)
// Long: float(123.123456789)

nextFloat() method

this method allows you to generate a random float number between zero and one.

$randomizer = new \Random\Randomizer();

$randomizer->nextFloat(); // 0.123456789
$randomizer->nextFloat(); // 0.987654321

Fetch class constants dynamically

Up until now, PHP did support dynamic or computed functions and properties.

$baz = 'foo';

$$baz = 'bar';
^
// $foo
// Calling class functions dynamically
class Foo
{
public function hello()
{
echo 'Hello world!';
}
}
$dynamicMethod = 'hello';
$a = new Foo();
$a->{$dynamicMethod}(); //prints 'Hello!'

However, calling class constants dynamically was not possible yet. But, not anymore! In PHP 8.3, you can expect this feature to be present.

class Foo 
{
public const BAR = 'bar';
}

$dynamicConstant = 'BAR';

echo Foo::{$dynamicConstant};
//prints 'bar'

In case you are trying to access a constant that does not exist, it will throw an error like mentioned below.

class Foo {}

$bar = 'BAR';
echo Foo::{$bar};
// Error: Undefined constant Foo::BAR

Unserialize() error handling improvement

PHP’s current error reporting unserialize() is very inconsistent, making it hard to reliably handle errors that occur during unserialization. In PHP 8.3 new \UnserializationFailedException will be added. Also, improve error handling.

Let’s see an example:

try {
set_error_handler(static function ($severity, $message, $file, $line) {
throw new \ErrorException($message, 0, $severity, $file, $line);
});
$result = unserialize($serialized);
} catch (\Throwable $e) {
// Unserialization failed. Catch block optional if the error should not be handled.
} finally {
restore_error_handler();
}

Here, you needed to set an error handler, catch exceptions, and restore the error handler. You can’t even guarantee that the restore error handler option will function seamlessly in case of an exception. It can be a lot of hassle.

However, the new PHP 8.3 comes with an improved unserialize( ) function. Thus, if your unserialize( ) method fails, it will throw an UnserializationFailedException. The E_WARNING or E_NOTICE will be converted into exceptions, wrapped in an instance of UnserializationFailedException. It makes catching and handling errors much easier.

So, instead of writing the aforementioned snippet of code, you can use the unserialize method like this:

try {
$result = unserialize('B:2:"jon";');
var_dump($result);
// Do something with the $result.
} catch (\UnserializationFailureException $e) {
// unserialization failed.
}

Of course, there are other changes in version 8.3 that we will discuss in the next articles.

I hope you enjoyed this article. If so, please clap for me :)

--

--