What’s new in PHP 8.3
On November 23, 2023, PHP 8.3 is set to launch, featuring enhancements to readonly classes, the introduction of the json_validate() function, expansions to the recently incorporated Randomizer class, stack overflow detection, and various other improvements.
Readonly improvements
This Request for Comments (RFC) suggested two modifications, but only one gained approval: the ability to reinitialize readonly properties during cloning. While this might seem significant, it’s essential to note that this RFC specifically targets a narrow, yet crucial, scenario – the alteration of property values within __clone() to facilitate the deep cloning of readonly properties.
class PHP {
public string $version = '8.2';
}
readonly class Foo {
public function __construct(
public PHP $php
) {}
public function __clone(): void {
$this->php = clone $this->php;
}
}
$instance = new Foo(new PHP());
$cloned = clone $instance;
// For PHP<8.3 Fatal error: Cannot modify readonly property Foo::$php
// With PHP 8.3 this is now possible!!
$cloned->php->version = '8.3';
[RFC] Class constants with explicit types
It is now possible to provide type hints for class constants:
class Foo
{
const string BAR = 'PHP8.3';
}
[RFC] Introduced #[\Override]
attribute
The recently introduced #[Override]
attribute serves as a declaration of the programmer’s intent. Essentially, it communicates, “I am aware that this method is overriding a parent method. If this ever changes, kindly bring it to my attention.”
abstract class Foo
{
public function someMethod(): string
{
return 'PHP8.2';
}
}
final class Bar extends Foo
{
#[Override]
public function someMethod(): string
{
return 'PHP8.2'; // The overridden method
}
}
[RFC] Recently added function, json_validate()
Before, the sole method to validate if a string constituted valid JSON was to decode it and check for any resulting errors. The newly introduced json_validate()
function proves advantageous when you solely need to determine the validity of the input as JSON, as it consumes less memory than decoding the string.
var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true
[RFC] Additions to the Randomizer
In PHP 8.2, the introduction of the new Randomizer class comes with a few minor additions:
Randomizer::getBytesFromString(string $string, int $length): string
This function enables the creation of a string with a specified length, comprising randomly chosen bytes from a given input string.
$randomizer = new \Random\Randomizer();
$temperature = $randomizer->getFloat(
-89.2,
56.7,
\Random\IntervalBoundary::ClosedOpen,
);
The getFloat()
function produces a float value within the range of $min
and $max
. You can specify whether the boundaries, $min
and $max
, are inclusive using the IntervalBoundary
enum. A setting of “Closed” includes the value, whereas “Open” excludes it.
Fetching dynamic class constants
In PHP 8.3, you can retrieve constants using a more dynamic syntax:
class Foo {
const PHP = 'PHP 8.3';
}
$searchableConstant = 'PHP';
var_dump(Foo::{$searchableConstant});
Related reads: