Free Online Toolbox for developers

PHP 8.3: new features and release date


What is the projected release date for PHP 8.3?

PHP 8.3 will be released on November 23, 2023.

It’s coming, let’s see what’s new together.


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 (memory and processing) than json_decode().

Learn more: json_validate()


Readonly amendments

Readonly properties will be able to be reinitialized during cloning, throughout the execution of the __clone() magic method call. It is allow deep cloning readonly properties.

class Bar {
    public function __construct(
        public readonly DateTime $foo
    ) {}
 
    public function __clone()
    {
        $this->foo = clone $this->foo; // No error
    }
}

Learn more: Readonly amendments


Dynamic class constant fetch

PHP implements various ways of looking up members name but this was not the case for the class constants.

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

Learn more: Dynamic class constant fetch


Improved unserialize() error handling

The error reporting in PHP’s unserialize() function is currently highly inconsistent, which makes it challenging to effectively handle errors that arise during the process of unserialization.

The specific type of error triggered by a malformed input string in PHP can vary, leading to the generation of an E_NOTICE, an E_WARNING, or the throwing of an arbitrary Exception or Error (sometimes even a combination of these possibilities).

  • Add wrapper Exception ‘UnserializationFailedException’: This enables to use a single catch(\UnserializationFailedException $e) to catch all possible Throwable
  • Increase the error reporting severity in the unserialize() parser.

Learn more: Improved unserialize() error handling


Randomizer additions

Three new methods will be added to \Random\Randomizer and one enum. These methods implement commonly useful operations that are either verbose or very difficult to implement.

namespace Random;
 
final class Randomizer {
    // […]
    public function getBytesFromString(string $string, int $length): string {}
    public function nextFloat(): float {}
    public function getFloat(
        float $min,
        float $max,
        IntervalBoundary $boundary = IntervalBoundary::ClosedOpen
    ): float {}
}
 
enum IntervalBoundary {
    case ClosedOpen;
    case ClosedClosed;
    case OpenClosed;
    case OpenOpen;
}

Learn more: Randomizer Additions


Typed class constants

PHP 8.3 will finally introduce typed class constants! This new feature aims to minimize bugs and confusion that often arise when child classes override constants from parent classes.

Typed class constants can now be declared in various entities, including classes, interfaces, traits, and enums.

enum E {
    const string BAR = "Test1";
}
 
trait T {
    const string BAR = E::BAR;
}
 
interface I {
    const string BAR = E::BAR;
}
 
class C implements I { 
    const string BAR = E::BAR;
}

Learn mode: Typed class constants


More Appropriate Date/Time Exceptions

Specific exceptions and errors for Date/Time extensions are added, where this makes sense.

Currently, there are warnings/errors or generic “Exception” or “Error” types. This lack of specificity makes it difficult to catch Date/Time exceptions effectively.

Learn more: More Appropriate Date/Time Exceptions


This page lists all the current RFCs for PHP 8.3.




Leave a Reply