What’s New in PHP 8.3? Explore Typed Constants, Dynamic Class Constants, and More
This article reviews the most important PHP 8.3 enhancements—including typed class constants, dynamic class constant access, a native json_validate() function, improved unserialize() error handling, read‑only property cloning support, and typed constants—showing code examples and practical usage tips.
Introduction
PHP 8.3 adds a collection of new language features and deprecates several older ones, making the language more elegant and developer‑friendly. The following sections explain the most notable additions and how to use them in real code.
Major New Features
Typed class constants
Dynamic class constant and enum member access
Native json_validate() function
Random extension mb_str_pad() function #[\Override] attribute
Additional PHP sockets options
Support for new cURL 7.87 options and constants
Anonymous read‑only classes
Negative indices in arrays
1. Json Validation
Instead of using json_decode() just to check validity, PHP 8.3 provides json_validate() which returns a boolean indicating whether a string is valid JSON.
$json = '{ "example": "title" }';
$is_json = false;
if (json_validate($json)) {
// Valid JSON.
$is_json = true;
}
// Or a shorter form
$is_json = json_validate($json);Most developers previously relied on json_decode() , which incurs unnecessary memory and CPU overhead when only validation is required.
2. Improved unserialize() Error Handling
The previous error handling for unserialize() was inconsistent, sometimes emitting notices, warnings, or throwing unpredictable exceptions. PHP 8.3 introduces a more reliable approach.
try {
set_error_handler(static function ($severity, $message, $file, $line) {
throw new \ErrorException($message, 0, $severity, $file, $line);
});
$result = unserialize($serialized);
} catch (\Throwable $e) {
// Optional handling of the unserialization error.
} finally {
restore_error_handler();
}
var_dump($result);After the change you can use a custom unserialize() wrapper that throws a dedicated exception.
function unserialize(string $data, array $options = []): mixed {
try {
// Existing unserialization logic.
} catch (\Throwable $e) {
throw new \UnserializationFailedException(previous: $e);
}
}
class UnserializationFailedException extends \Exception {}3. Fetching Class Constants Dynamically
Prior to PHP 8.3, class constants could only be accessed with a literal name. The new version allows variable‑based access.
class StatusCodes {
const OK = 200;
const NOT_FOUND = 404;
const INTERNAL_ERROR = 500;
const UNAUTHORIZED = 401;
const FORBIDDEN = 403;
}
// Direct access
echo StatusCodes::OK; // 200
// Dynamic access
$var = "OK";
echo StatusCodes::{$var}; // 200
$var = "NOT_FOUND";
echo StatusCodes::{$var}; // 404This makes it possible to retrieve constants using variables, which simplifies certain reflection‑oriented patterns.
4. Introducing Read‑Only Modifications
PHP 8.3 relaxes the restriction on modifying read‑only properties during cloning. The proposal enables re‑initialisation of read‑only attributes inside the __clone() method.
// Before PHP 8.3 – cannot modify read‑only property in __clone()
class Example {
public readonly $readOnlyProp = 'Initial Value';
}
$instance = new Example();
$instance->readOnlyProp = 'New Value'; // Fatal error
// After PHP 8.3 – allowed via explicit handling
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); // Works, property becomes uninitialized
}
}
$foo = new Foo('Test', 'Example');
$foo2 = clone $foo; // No error, $foo2->example is cloned, $foo2->foo is uninitialized5. Typed Constants
Before PHP 8.3, class constants could not declare a type. The new syntax allows typed constants, improving static analysis and readability.
// Untyped constant (pre‑8.3)
class Example {
const HTTP_STATUS_OK = 200;
}
// Typed constant (PHP 8.3+)
class Example {
const int HTTP_STATUS_OK = 200;
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
