PHP 8.3 introduces several new features, optimizations, and deprecations that enhance the language’s functionality, performance, and security. Here’s a detailed overview of the updates:
- Readonly Classes
- Readonly Keyword: PHP 8.2 introduced readonly properties, and PHP 8.3 extends this concept to entire classes. Declaring a class as
readonlymeans all its properties are automatically readonly, ensuring they can only be initialized once.
- readonly class Config {
public string $host;
public int $port;
public function __construct(string $host, int $port) {
$this->host = $host;
$this->port = $port;
}
}
- json_validate() Function
- New Function:
json_validate()is introduced to check if a string is a valid JSON without decoding it, which can be useful for scenarios where you need to validate JSON input.
$json = ‘{“name”: “John”, “age”: 30}’;
if (json_validate($json)) {
echo “Valid JSON!”;
} else {
echo “Invalid JSON!”;
}
New Date and Time Functions
- PHP 8.3 brings enhancements to date and time handling, such as:
- timezone_from_offset(): This function returns a timezone object based on a UTC offset.
- Enhanced timezone handling: Improved accuracy and new functions for dealing with timezones.
$timezone = timezone_from_offset(7200); // UTC+2
echo $timezone->getName(); // Outputs: Etc/GMT-2