Backend Development 5 min read

New Features in PHP 8.3: Typed Class Constants, json_validate, and Other Improvements

PHP 8.3, released on November 23, 2023, introduces typed class constants, a high‑performance json_validate function for JSON payload validation, and numerous minor enhancements such as Randomizer tweaks, read‑only property cloning, the #[Override] attribute, and support for anonymous read‑only classes.

php中文网 Courses
php中文网 Courses
php中文网 Courses
New Features in PHP 8.3: Typed Class Constants, json_validate, and Other Improvements

PHP 8.3 will be released on November 23, 2023. It adds typed class constants, a new helper function json_validate for checking JSON payloads, and several small improvements to the Randomizer class, ini parsing, and other areas.

The PHP team will publish a minor version at the end of the month. The new version mainly contains improvements and features that are not particularly relevant to end‑users.

Typed Class Constants

We all use constants at some point. Until now the type of a constant was inferred from its value, which meant that inheritance or interface implementation could change the type. In short, the new feature works as follows:

<code>class Foo
{
    public int BAZ = 1;
}
</code>

Validate JSON Payloads

In almost every situation where JSON must be read, transformed, or written, it should be validated first. This can be done by performing a “test decode” to see if an exception is thrown, or by adding an else branch for invalid payloads.

For small payloads this may be acceptable, but decoding very large JSON strings into arrays just to check validity is not elegant and can cause memory or performance issues.

The new json_validate function promises to check whether a given string is valid JSON with higher performance and lower memory usage. Example:

<code>json_validate(string $json, int $depth = 512, int $flags = 0): bool
</code>

Further Improvements

As mentioned, PHP 8.3 also includes several other improvements. I list them here for completeness, although I consider them less important.

Randomizer improvements: small enhancements such as specifying a range for random floating‑point values or defining string length.

Read‑only revisions: allow read‑only properties to be re‑initialized when an object is cloned, which can be crucial for deep cloning.

#[Override] attribute: indicates the programmer’s intent to override a parent method and triggers a warning if the method does not actually override anything.

Anonymous read‑only classes: previously impossible, now supported.

Dynamic class constant fetching: PHP now permits reading dynamic class properties with braces, and with 8.3 it can also read constants (though this is discouraged).

Static properties in traits: re‑declare static properties inherited from a parent class, a mix of features that can be misused.

And more – see the official migration guide for a complete list.

Conclusion

As with every new release, there is no golden rule for whether to upgrade. We strongly recommend using the latest version because it usually fixes bugs and security issues from previous releases and provides better performance and features.

PHPjson validationRandomizertyped constants
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.