DEV Community

spO0q πŸ’
spO0q πŸ’

Posted on

PHP 8.3: a new major in town

PHP 8.3 is available with cool features you might not know yet.

You have now a built-in JSON validator

var_dump(json_validate('{ "test bad": { ')); // false
Enter fullscreen mode Exit fullscreen mode

You can now type constants

Pretty cool new feature:

class MyClass
{
    public const string MY_TABLE = 'my_table'; 
}
Enter fullscreen mode Exit fullscreen mode

Also note that the visibility of constants defined in interfaces is now checked (which wasn't the case before).

The following code will throw an error in PHP 8.3:

interface MyInterface {
    public const HOLY_CONST = 'constantly constant';
}

class MyClass implements MyInterface {
    private const HOLY_CONST = 'constantly constant';
}
Enter fullscreen mode Exit fullscreen mode

PHP can now detect stack overflows

Thanks to new built-in directives max_allowed_stack_size and reserved_stack_size (INI setting), PHP can detect when the program is about to overflow the stack.

The idea is to throw an exception before the stack overflows instead of letting the program exhaust the memory. This is automatic but you can still set custom values if you know what you're doing.

While these new mechanisms look efficient, it cannot prevent all kinds of stack overflows. However, this RFC aims to improve the developer experience in most cases.

Source: GitHub - php-src

You can catch more specific Date/Time exceptions

Catching Date/Time exceptions can be problematic is some cases, as they are not specific enough.

New exceptions such as DateMalformedIntervalStringException or DateRangeError will certainly improve the developer experience.

Source: rfc - datetime-exceptions

Improve readability with the #[\Override] attribute

By adding the #[\Override] attribute to a method, PHP will ensure that a method with the same name exists in a parent class or in an implemented interface. Adding the attribute makes it clear that overriding a parent method is intentional and simplifies refactoring, because the removal of an overridden parent method will be detected.

Source: PHP 8.3 release announcement

Nice!

Wrap this up

This post is obviously not exhaustive, so please check this page to get more details.

Top comments (0)