Backend Development 4 min read

New Features in PHP 8.3: Typed Class Constants, Dynamic Class Constants, Readonly Property Deep Clone, and json_validate() Function

The article introduces PHP 8.3’s major enhancements—including typed class constants, dynamic class constant access, deep‑clone support for readonly properties, and the new json_validate() function—while noting the upcoming end of PHP 8.0 support and providing practical code examples.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
New Features in PHP 8.3: Typed Class Constants, Dynamic Class Constants, Readonly Property Deep Clone, and json_validate() Function

PHP 8.3 has been officially released, and the active support period for PHP 8.0 has already ended; security support for PHP 8.0 will cease three days after the PHP 8.3 release, on 2023‑11‑26.

The main new features of PHP 8.3 are:

Typed class constants

// PHP < 8.3
interface I {
    // We may naively assume that the PHP constant is always a string.
    const PHP = 'PHP 8.2';
}

class Foo implements I {
    // But implementing classes may define it as an array.
    const PHP = [];
}

// PHP 8.3
interface I {
    const string PHP = 'PHP 8.3';
}

class Foo implements I {
    const string PHP = [];
}
// Fatal error: Cannot use array as value for class constant Foo::PHP of type string

Dynamic retrieval of class constants

// PHP < 8.3
class Foo {
    const PHP = 'PHP 8.2';
}

$searchableConstant = 'PHP';
var_dump(constant(Foo::class . "::{$searchableConstant}"));

// PHP 8.3
class Foo {
    const PHP = 'PHP 8.3';
}

$searchableConstant = 'PHP';
var_dump(Foo::{$searchableConstant});

Readonly property deep clone

The readonly attribute can now be modified once inside the magic __clone method, enabling deep cloning of readonly properties.

// PHP < 8.3
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;
// Fatal error: Cannot modify readonly property Foo::$php

// PHP 8.3
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;
$cloned->php->version = '8.3';

New json_validate() function

The json_validate() function checks whether a string is syntactically valid JSON more efficiently than using json_decode() followed by error checking.

// PHP < 8.3
function json_validate(string $string): bool {
    json_decode($string);
    return json_last_error() === JSON_ERROR_NONE;
}
var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

// PHP 8.3
var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

For more details, refer to the official release notes: https://www.php.net/releases/8.3/en.php

Did you find this article helpful? Liking and sharing are the greatest support!

backendprogrammingJSONReadOnlyPHP 8.3
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.