๐Ÿ˜
Websites

New Features in PHP 8.3: What Every Developer Should Know

07.01.2025
โ† All articles

PHP 8.3 was officially released at the end of 2023, marking another important milestone in the language's evolution. Rather than introducing revolutionary changes, this release focuses on practical improvements: features that increase code safety, catch errors at an earlier stage, and make day-to-day development smoother. Whether you work with Laravel, Symfony, or your own custom PHP project, understanding these additions will help you keep your code clean and reliable. Below we examine each significant feature in detail with real code examples and explain how the new approach differs from the old one.

Typed class constants

In earlier versions, class constants could not be given an explicit type, which often led to confusion and hard-to-trace bugs in large projects. PHP 8.3 now lets you assign a concrete type to a constant, preventing situations where an inheriting class accidentally redefines a constant with an incompatible type. This is especially valuable for teams working on a large codebase, because if an incorrect type is used, PHP throws an error immediately and the problem never reaches production unnoticed.

interface HasLimit {
    const int MAX_USERS = 100;
}

class Service implements HasLimit {
    // Error: cannot change int to bool
    // const bool MAX_USERS = true;
    const int MAX_USERS = 250; // Correct
}

This change also improves the self-documenting nature of code: a developer seeing the constant's type immediately understands its purpose. The key difference from older versions is that previously the type could only be expressed in a comment, whereas now it is enforced at the language level, making the contract between classes strict and predictable.

The json_validate() function

Previously, to check whether an incoming JSON string was valid, most developers reached for json_decode() and then inspected the errors. However, that approach unpacked the entire string into a PHP array in memory, which wasted resources when dealing with large payloads. PHP 8.3 introduces the dedicated json_validate() function, which only checks the validity of the string without decoding anything, so it runs faster and uses noticeably less memory.

$json = '{"name": "John", "age": 30}';

if (json_validate($json)) {
    $data = json_decode($json, true);
    // Work with the data
} else {
    echo "Invalid JSON";
}

This function is particularly useful on API endpoints, where you need to filter user-supplied data right away. If your goal is merely to validate and you do not yet need the decoded data, the new function works far more efficiently than the old approach and reduces server load under heavy traffic.

The #[Override] attribute

In large projects, when overriding a parent class method in a child, it is easy to make a typo in the name or to silently break the code after the method is renamed in the parent. The new #[Override] attribute explicitly tells PHP that a method genuinely overrides a parent method. If no such parent method exists, PHP raises an error immediately, so you catch the mistake before it ever reaches production.

class BaseController {
    public function handle(): void {}
}

class UserController extends BaseController {
    #[Override]
    public function handle(): void {
        // Overrides the parent method
    }
}

This attribute also sends a clear signal to anyone reading the code: the method was overridden deliberately, not by accident. During refactoring this feature is invaluable, because when the parent class interface changes, every related override automatically produces a warning and none of them slips through unnoticed.

Dynamic class constant fetch

Previously, to read a class constant by a dynamic name you had to use the constant() function, which made the code somewhat awkward. PHP 8.3 now lets you reference a constant name through a variable directly within curly braces, just as you would with methods or properties. This syntax makes the code more consistent and easier to read.

class Status {
    const ACTIVE = 'active';
    const BLOCKED = 'blocked';
}

$name = 'ACTIVE';
echo Status::{$name}; // 'active'

This addition is especially handy when working with enums and configuration classes, where a value often needs to be chosen dynamically. Compared to the old constant('Status::' . $name) approach, the new syntax is far more natural and less error-prone, since an IDE can analyze such code more effectively.

Randomizer improvements and other changes

The Randomizer class introduced in PHP 8.2 gained new methods in version 8.3. You can now use getBytesFromString() to generate a random string from a given set of characters, which is very convenient for creating passwords or tokens. In addition, the getFloat() and nextFloat() methods return reliable random floating-point numbers within a precisely defined range.

use Random\Randomizer;

$r = new Randomizer();
$token = $r->getBytesFromString(
    '0123456789abcdef', 16
);
echo $token; // e.g. 'a3f90c7b2e14d8a6'

Beyond that, PHP 8.3 marks a number of legacy features as deprecated; for example, calling get_class() with no arguments now produces a warning. These changes are necessary to keep the language clean going forward. As for whether it is worth upgrading, the answer is clear: yes, because in 8.3 the backward-compatibility breakage is minimal while the new features bring tangible value.

Is it worth upgrading and what about compatibility

Moving to PHP 8.3 is painless for most projects, since there are no major breaking changes. If you are already on 8.1 or 8.2, your code will run almost untouched while you gain the latest security and performance improvements. Before upgrading, we recommend running your project in a test environment and reviewing the warnings about deprecated functions. Overall, PHP 8.3 serves as a reliable, fast, and secure foundation for modern web development, and there is little reason to delay the move.

Related articles

๐ŸŒพ Agriculture and Agribusiness Website: Product Catalog and B2B Sales โค๏ธ Charity Foundation Website: Transparent Fundraising and Donor Trust ๐ŸŽ‰ Wedding Venue and Banquet Hall Website: Event Planning and Online Booking ๐Ÿš™ Car Rental Website: Vehicle Catalog, Price Calculator, and Online Booking
๐ŸŒ Language
๐Ÿ‡บ๐Ÿ‡ฟ O'zbek ๐Ÿ‡บ๐Ÿ‡ฟ ะŽะทะฑะตะบ ๐Ÿ‡ท๐Ÿ‡บ ะ ัƒััะบะธะน ๐Ÿ‡ฌ๐Ÿ‡ง English โœ“