Backend Development 27 min read

New Features and Improvements in PHP 8.3

PHP 8.3 introduces a suite of enhancements—including readonly class cloning, the json_validate() function, expanded Randomizer methods, typed class constants, anonymous readonly classes, dynamic constant access, refined date/time exceptions, improved unserialize() error handling, trait static properties, stack‑overflow detection, invariant constant visibility, and numerous other updates—aimed at making web development more efficient, secure, and expressive.

php中文网 Courses
php中文网 Courses
php中文网 Courses
New Features and Improvements in PHP 8.3

PHP 8.3 is the latest version of the popular server‑side scripting language, bringing many new functions and improvements designed to simplify web development and boost performance. These enhancements include readonly classes, the json_validate() function, an extended Randomizer class, typed class constants, anonymous readonly classes, dynamic class‑constant access, better date/time exceptions, improved unserialize() error handling, trait static properties, stack‑overflow detection, invariant constant visibility, and many other refinements.

1. Readonly Class Improvements

PHP 8.3 allows readonly properties to be re‑initialized during cloning, enabling deep cloning of objects with readonly attributes. The following example demonstrates a readonly class with a DateTime property that can be reset in __clone() :

<code>readonly class Post {
    public function __construct(public DateTime $createdAt) {
        // constructor logic
    }

    public function __clone() {
        $this->createdAt = new DateTime();
        // now re‑initializes the readonly property
    }
}
</code>

When cloning, the __clone() method can reassign the readonly $createdAt property, allowing deep copies without breaking immutability guarantees.

2. New Function json_validate()

The json_validate() function provides a fast, direct way to check whether a JSON string is syntactically valid without decoding it. Example:

<code>$jsonString = '{ "name" : "xx" , "age" : 30 , "city" : "xxx" }';
if (json_validate($jsonString)) {
    echo "JSON string is valid.";
} else {
    echo "JSON string is invalid.";
}
</code>

The function also accepts custom depth and flags, e.g., json_validate($jsonString, 512, JSON_THROW_ON_ERROR) , offering fine‑grained control over validation.

3. Expanded Randomizer Class

The Randomizer class now includes methods such as getBytesFromString() and getRandomInteger() , allowing developers to generate random bytes from a string or random integers within a specified range:

<code>use Randomizer;

$string = "Hello, World!";
$length = 10;
$randomBytes = Randomizer::getBytesFromString($string, $length);
var_dump($randomBytes);
</code>
<code>use Randomizer;
use IntervalBoundary;

$min = 10;
$max = 20;
$randomInteger = Randomizer::getRandomInteger($min, $max, IntervalBoundary::Closed);
echo $randomInteger;
</code>

These methods make it easier to produce controlled random data for testing, security tokens, or other use cases.

4. Typed Class Constants

Typed class constants let developers declare constants with explicit data types, improving type safety and readability. Example:

<code>class MathOperations {
    public const PI: float = 3.14159;
    public const MAX_ITERATIONS: int = 1000;
}
</code>

Another example shows a configuration class with typed constants for timeout and logging options.

5. Anonymous Readonly Classes

PHP 8.3 adds support for anonymous classes marked as readonly, enabling lightweight immutable objects without a formal class declaration:

<code>$person = new class {
    public function __construct(public string $name, public int $age) {}
};

$john = new $person('xx', 30);
echo $john->name; // outputs: xx
echo $john->age;  // outputs: 30
</code>

Anonymous readonly classes can also be used for temporary data structures.

6. Dynamic Class Constant Access

Developers can now retrieve class constants dynamically using variable names, e.g., Configuration::{$constantName} , or via a helper function that returns the constant value based on a string.

7. More Appropriate Date/Time Exceptions

New dedicated exceptions such as DateRangeError and DateMalformedIntervalStringException allow finer‑grained error handling for date‑time operations.

8. Improved unserialize() Error Handling

The unserialize() function now provides consistent error handling, allowing developers to check for E_WARNING warnings or catch UnserializeException in a try‑catch block.

9. Traits with Static Properties

Traits that contain static properties now maintain separate storage per using class, preventing shared state between classes that use the same trait.

10. Stack‑Overflow Detection

Two new php.ini directives— zend.max_allowed_stack_size and zend.reserved_stack_size —enable detection and prevention of stack overflows, improving application stability.

11. Invariant Constant Visibility

The invariant keyword ensures that class constants retain their visibility across inheritance hierarchies, preventing accidental changes in visibility.

12. Assertion String Evaluation Cleanup

String‑based assertions (e.g., assert('is_numeric($value)') ) are deprecated in favor of direct expression assertions ( assert(is_numeric($value)) ), enhancing security and maintainability.

13. Improved FFI\CData:void

When calling C functions that return void via FFI, the return value is now represented as null instead of an FFI\CData&lt;void&gt; instance.

14. posix_getrlimit() Parameter Enhancement

The function now accepts optional parameters to retrieve a single resource limit, such as the soft limit for maximum open files or the hard limit for CPU time.

15. gc_status() Improvements

Eight new fields— running , protected , full , buffer_size , application_time , collector_time , destructor_time , and free_time —provide detailed insight into PHP's garbage‑collection process.

16. class_alias() Support for Internal Classes

Developers can now create aliases for internal PHP classes, e.g., class_alias('DateTime', 'MyDateTime'); , and instantiate them using the alias.

17. mysqli_poll() Error Handling

If mysqli_poll() is called without providing read or error arrays, a ValueError is thrown, enforcing proper usage.

18. array_pad() Enhancement

The previous limit of adding at most 1,048,576 elements in a single call has been removed, allowing arrays to be padded with arbitrarily many elements.

19. Removal of opcache.consistency_checks INI Directive

The opcache.consistency_checks directive has been removed, simplifying OPCache configuration.

20. Correct Handling of Negative Decimal Parameter in number_format()

Negative values for the decimal argument now round numbers to the specified number of significant digits before the decimal point, e.g., number_format(1234.56789, -2) yields 1,200 .

These twenty new features and improvements make PHP 8.3 a more powerful, reliable, and developer‑friendly version for building modern web applications.

phpcode examplesnew featuresPHP 8.3
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.