Share

PHP 8.3 Features: Check What’s New and What’s Changed?

PHP 8.3 Released: Check What's New and What's Changed?

Get ready to explore the fantastic world of PHP 8.3, where coding gets a cool upgrade! In this super-detailed guide, we’ll take you through impressive PHP 8.3’s features, performance boosters, and what the friendly coding community is buzzing about. Whether you’re a coding pro or just starting, let’s discover the secrets of PHP 8.3 together.

About PHP 8.3 Update

About PHP 8.3

PHP 8.3 is the latest upgrade in the coding world which was released on 23 November 2023. It adds cool new features, makes things run faster, and makes sure your code is more secure. One exciting thing it does is use a Just-In-Time (JIT) compiler to speed up how your code works. It also brings in something called enumerations to make your code design better. Whether you’re a pro coder or just starting out, PHP 8.3 has got something for everyone, making coding more fun and exciting!

PHP 8.3 Features and Improvements

1. Typed Class Constants

We’ve had the ability to declare types for class properties since PHP 7.4. Despite various modifications in PHP over the years, it hasn’t yet been applied to constants until now.

In PHP 8.3, class constants, including interface, trait, and enum constants can be typed.

For Example:

class Test {
    const string TEST_CONSTANT = 'test';
}

2. The json_validate() function

Initially, the only way to figure out whether a string was valid JSON was to decode it and detect the errors if there were any. However, the new json_validate() function takes less memory than decoding the string and can be helpful if you simply need to know whether the input is valid JSON or not.

For Example:

json_validate(string $json, int $depth = 512, int $flags = 0): bool

3. Tweaks in readonly

PHP 8.1 introduced the option to specify individual class properties as readonly. The ability to apply the attribute to an entire class was introduced in PHP 8.2. However, many developers felt that the limits caused while working with classes that contained such characteristics limited useful programming.

Two proposals were proposed in an RFC for modifying readonly behavior:

  1. Allow non readonly classes to extend readonly classes.
  2. When cloning, allow readonly properties to be initialized.

This second proposal has been accepted into PHP 8.3. The new approach enables the reinitialization of instances of a class with readonly attributes within the __clone magic method.

For Example:

readonly class Student
{
    public function __construct(
        public DateTime $createdAt,
    ) {}
    
    public function __clone()
    {
        $this->createdAt = new DateTime(); 
        // This is allowed,
        // even though `createdAt` is a readonly property.
    }
}

4. New #[Override] Attribute

The programmer’s intent is displayed via the new #[Override] attribute. In simple terms, it says, “I understand that this way is overriding a parent method. Please inform me if that were to change at any point“.

For Example:

class A {
    protected function overrideTest(): void {}
}

// This will work because ovrTest() 
// can be found in the parent class
class B extends A {
    #[\Override]
    public function overrideTest(): void {}
}

// This will fail because overrideBest() 
// (probably a typo) is not in the parent
class C extends A {
    #[\Override]
    public function overrideBest(): void {}
}

5. Dynamic Class Constant Fetch

Fetching class constants with variable names has proven to be a little more complex than with other properties in PHP code. You may have done it using the constant() function in a manner similar to this before PHP 8.3.

For Example:

class Person 
{
    const NAME = 'John';
}
$name = 'NAME';
 
// Instead of this:
constant(Person::class . '::' . $name);
// You can now do this:
Person::{$name};

5. New getBytesFromString() Method

This new PHP 8.3 feature helps to create random strings with a pre-selected set of characters. It’s getBytesFromString() method, which was included in the Random extension, makes it simple to accomplish now.

This new approach is straightforward, you give it a string of characters to work with and tell it how many of them to utilize. After that, until the string reaches the desired length, the function will randomly select bytes from it.

For Example:

$rando = new Random\Randomizer();
$alpha = 'ABCDEFGHJKMNPQRSTVWXYZ';

$rando->getBytesFromString($alpha, 8); //  "MBXGWLAV"
$rando->getBytesFromString($alpha, 8); //  "LESPMGWK"
$rando->getBytesFromString($alpha, 8); //  "NVHWXCPU"

It’s possible that the random output’s specified length will contain more bytes than the input string:

$rando = new Random\Randomizer();
$nums = '12345';

$rando->getBytesFromString($nums, 8); //  "43121523"

6. New getFloat() and nextFloat() Methods

The next feature includes two new methods, getFloat() and nextFloat(), that generate random float values, further building on the Random extension.

Following the minimum and maximum values, a third parameter is also accepted by the getFloat() method.

If the Enum is not specified as the third option when calling getFloat(), IntervalBoundary::ClosedOpen is the default.

Example on getFloat():

$rando = new Random\Randomizer();

printf(
    "Lat: %+.6f Long: %+.6f",
    $rando->getFloat(-90, 90, \Random\IntervalBoundary::ClosedClosed),

    // -180 will not be used 
    $rando->getFloat(-180, 180, \Random\IntervalBoundary::OpenClosed),
);

the new nextFloat() function is practically the same as using getFloat() when requesting a random value that falls between 0 and less than 1.

Example on nextFloat():

$rando = new Random\Randomizer();

$rando->nextFloat(); // 0.4679027143784

Additional Minor Changes in PHP 8.3 Features List

Several further new features and small adjustments are also included in this PHP version. We’ll include these below along with links for further resources.

PHP 8.3 Deprecations

Some PHP functions and settings are marked for removal with each new release. These features are no longer supported for use and generate warnings in numerous logs when they appear in executing code.

  1. The U_MULTIPLE_DECIMAL_SEPARATORS constant is deprecated in favor of U_MULTIPLE_DECIMAL_SEPARATORS.
  2. When a negative number n is assigned to an empty array, It will now make sure that the next index is n + 1 instead of 0.
  3. Changes to the range() function.
  4. SQLite3: Default error mode set to exceptions.
  5. The 3MT_RAND_PHP Mt19937 variant is deprecated.
  6. ReflectionClass::getStaticProperties() is no longer nullable.
  7. More Appropriate Date/Time Exceptions.
  8. INI settings assert.active, assert.bail, assert.callback, assert.exception, and assert.warning are deprecated.
  9. Changes in re-declaration of static properties in traits.
  10. Calling get_class() and get_parent_class() without arguments have been deprecated.

Conclusion

Simply put, PHP 8.3 transforms web development, making it faster and easier for developers. With improved performance and simpler coding, PHP ensures efficient debugging and seamless integration with modern tools. Real-world examples prove its impact, making it a go-to for better project performance. Staying updated with every version of PHP is crucial for mastering the ever-changing web development landscape. It’s not just the latest; PHP 8.3 is a game-changer, shaping the future of coding.

Ever wondered about spicing up your WordPress blog without the hassle of plugins? Discover our exclusive guide on “Display Reading Time on a WordPress Blog Without a Plugin” It’s a simple trick to elevate your blog’s user experience.

FAQs: Answering Your Curious Questions

Q.1 Why is PHP 8.3 a big deal?

It brings speed, clarity in code, and enhanced security, making it a game-changer for developers.

Q.2 Is upgrading to PHP 8.3 from an older version complicated?

Not at all! It is designed to smoothly transition from older versions, making the upgrade process friendly for developers.

Q.3 Are there any backward compatibility issues when migrating to PHP 8.3?

While it brings new features, it’s crucial to review the official PHP migration guide to identify any potential backward compatibility issues. Testing your code in a controlled environment before the upgrade is recommended to ensure a smooth transition.

Q.4 What security features does PHP 8.3 bring?

It introduces new security features to protect your code from common vulnerabilities, ensuring a more robust coding environment.

Q.5 Can I use attributes on class constants in PHP 8.3?

Yes, it allows the use of attributes on class constants. This feature enhances metadata handling in your code and provides more flexibility in documenting and organizing your classes.

Leave a Reply

Your email address will not be published. Required fields are marked *


This website uses cookies to ensure you get the best experience on our website. By continuing to use this site, you agree to the use of cookies in accordance with our Cookie Policy.