Backend Development 6 min read

New PHP 8.3 Features: JSON Validation, Improved unserialize Error Handling, Dynamic Class Constants, Readonly Property Cloning, and Typed Constants

PHP 8.3 introduces native JSON validation, improved unserialize error handling, dynamic class constant access, the ability to reinitialize readonly properties during cloning, and typed class constants, providing developers with more efficient and expressive tools for backend development.

php中文网 Courses
php中文网 Courses
php中文网 Courses
New PHP 8.3 Features: JSON Validation, Improved unserialize Error Handling, Dynamic Class Constants, Readonly Property Cloning, and Typed Constants

PHP now supports native desktop programming and adds several powerful features in version 8.3 that simplify common backend tasks.

JSON Validation

When developing new projects we often need to verify whether a string is valid JSON. PHP 8.3 adds a native json_validate() function that returns a boolean without the overhead of json_decode() . Example usage:

<code>$json = '{ "example": "title" }';
$is_json = false;
if (json_validate($json)) {
    // 有效的JSON
    $is_json = true;
}

// 或者更好的方法
$is_json = json_validate($json);
</code>

Most developers previously used json_decode() , which consumes unnecessary memory and CPU.

Improved unserialize() Error Handling

The previous unserialize() produced inconsistent errors such as E_NOTICE , E_WARNING , or unpredictable exceptions. PHP 8.3 introduces a consistent exception UnserializationFailedException . Example of the old approach:

<code>try {
    set_error_handler(static function ($severity, $message, $file, $line) {
        throw \ErrorException($message, 0, $severity, $file, $line);
    });

    $result = unserialize($serialized);
} catch (\Throwable $e) {
    // 如果要处理非序列化错误,可选的catch代码块。
} finally {
    restore_error_handler();
}

var_dump($result);
</code>

New unified implementation:

<code>function unserialize(string $data, array $options = []): mixed
{
    try {
        // 现有的非列化逻辑在这里发生。
    } catch (\Throwable $e) {
        throw new \UnserializationFailedException(previous: $e);
    }
}
class UnserializationFailedException extends \Exception
{
}
</code>

Dynamic Class Constant Access

Before PHP 8.3 class constants could only be accessed with a literal name. The new version allows variable‑based access. Example:

<code>class StatusCodes {
    const OK = 200;
    const NOT_FOUND = 404;
    const INTERNAL_ERROR = 500;
    const UNAUTHORIZED = 401;
    const FORBIDDEN = 403;
}

// 访问常量
echo StatusCodes::OK; // 输出: 200
echo StatusCodesNOT_FOUND; // 输出: 404
echo StatusCodes::INTERNAL_ERROR; // 输出: 500
echo StatusCodes::UNAUTHORIZED; // 输出: 401
echo StatusCodes::FORBIDDEN; // 输出: 403
</code>

This enables dynamic retrieval of constants at runtime.

Readonly Property Re‑initialization in Cloning

Readonly properties could not be re‑initialized inside __clone() before 8.3. The new feature permits resetting readonly properties during cloning.

Before 8.3:

<code>class Example {
    public readonly $readOnlyProp = '初始值';
}

$instance = new Example();
$instance->readOnlyProp = 'New Value'; // 这行不通
class Example {
    public function __construct(
        public readonly string $readOnly,
    ) {}
  public function __clone()
    {
        $this->readOnlyProp = clone $this->readOnlyProp; // 不起作用会出现错误。
    }
}
</code>

After 8.3:

<code>class Foo {
    public function __construct(
        public readonly string $example,
    public readonly string $foo
    ) {}

    public function __clone()
    {
        $this->example = clone $this->example; // Works.
        $this->cloneFoo();
    }

    private function cloneFoo()
    {
        unset($this->foo); // 也可以使用
    }
}

$foo = new Foo('Test', 'Example');

$foo2 = clone $foo;
// 没有错误,Foo2::$example被深度克隆,而Foo2::$foo变为未初始化。
</code>

Typed Class Constants

PHP 8.3 allows specifying a type for class constants, for example:

<code>class Example
{
    const HTTP_STATUS_OK:int = 200;
}
</code>

These additions make PHP desktop and backend programming more powerful and enjoyable.

PHPReadOnlyjson validationtyped constants8.3unserialize
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

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.