New Features and Improvements in PHP 8.3
PHP 8.3, released on November 23 2023, introduces numerous backend enhancements such as improved readonly class cloning, typed class constants, the #[Override] attribute, negative array indexes, anonymous readonly classes, new functions like json_validate and mb_str_pad, Randomizer upgrades, and refined error handling, all aimed at boosting developer productivity.
PHP 8.3, released on November 23 2023, adds a wide range of language improvements that enhance the backend development experience.
Enhanced readonly classes – cloning now re‑initialises readonly properties, allowing deeper object copies. Example:
class Article {
public readonly DateTime $publishedOn;
public function __construct(DateTime $publishedOn) {
$this->publishedOn = $publishedOn;
}
public function __clone() {
// PHP 8.3 allows
$this->publishedOn = new DateTime();
}
}Typed class constants – constants can now have explicit types, improving type safety:
class Config {
const API_KEY = 'your-api-key';
}#[Override] attribute – declares intentional method overrides, catching errors when a parent method is renamed or removed:
abstract class BaseClass {
public function defaultMethod(): int {
return 1;
}
}
final class DerivedClass extends BaseClass {
#[Override]
public function defaultMethod(): int {
return 2; // intentional override
}
}Negative array indexes – adding items to an empty array with a negative index now places subsequent items at the next negative index, making behaviour predictable.
$array = [];
$array[-1] = 'first';
$array[] = 'second';
var_export($array); // [ 'first', 'second' ]Anonymous readonly classes – support for creating immutable objects on the fly:
$anonymousClass = new readonly class {
public function __construct(
public string $name = 'Anonymous',
) {}
};New function json_validate() – validates JSON strings without decoding, saving memory:
$jsonString = '{"name": "Alice", "age": 30}';
$isJsonValid = json_validate($jsonString);Randomizer enhancements – added methods to generate random bytes from a string and random floats within a range:
$randomizer = new Randomizer();
$randomBytes = $randomizer->getBytesFromString('abcdef', 4);
$randomFloat = $randomizer->getFloat(0.0, 1.0);Dynamic class constant access – new syntax simplifies retrieving constants dynamically:
class Setting {
const MODE = 'production';
public static function getCurrentMode() {
return static::MODE;
}
}
$currentMode = Setting::getCurrentMode();More specific Date/Time exceptions – dedicated exception classes provide clearer error messages for date‑time functions.
Improved unserialize() error handling – always throws E_WARNING on failure, offering consistent diagnostics.
Range() function upgrades – now throws TypeError for invalid bounds and ValueError for invalid steps, making its behavior more intuitive.
Traits with static properties – static properties in traits are now re‑declared separately for each class, aligning trait behavior with normal class static properties.
Stack overflow detection – a new INI directive detects stack overflows, preventing segmentation faults in deep recursion scenarios.
New mb_str_pad() function – fills multibyte strings, essential for correct padding of UTF‑8 text.
Magic method closures and named arguments – magic methods can now be turned into closures and accept named parameters, expanding their flexibility.
Immutable constant visibility fix – resolves a bug in interface constant visibility checks, ensuring consistent visibility rules.
Deprecations – PHP 8.3 deprecates several outdated functions such as mb_strimwidth() and ldap_connect() , encouraging migration to newer alternatives.
Overall, PHP 8.3 provides developers with richer language features, better performance, and more robust error handling, making it a significant step forward for modern backend development.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
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.