Programster's Blog

Tutorials focusing on Linux, programming, and open-source

What's New In PHP 8.3

PHP
  1. Random String Generation (getBytesFromString)
  2. JSON Validate
  3. Cloning Objects - Handling Of Readonly Properties
  4. Typed Class Constants
  5. Anonymous Readonly Classes
  6. Behaviour Change - Negative indices in arrays
  7. SQLite3 Exceptions By Default
  8. References

Random String Generation

The getBytesFromString method was added to Randomizer, allowing you to easily generate a random string using a character allow list. E.g. for a random alphanumeric 24 character string you could use:

$allowedChars =  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$randomToken = Randomizer::getBytesFromString($allowedChars, 24);

Alternatively, if you wanted the length of the string to be randomly between 24 and 32 characters:

$allowedChars =  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$numChars = rand(24,32);
$randomToken = Randomizer::getBytesFromString($allowedChars, $numChars);

JSON Validate

The json_validate function was added in this release which returns a boolean indicating whether the given string is syntactically valid JSON. This is fantastic for those of us who work with APIs and had been relying on making use of JSON_THROW_ON_ERROR to throw an exception if the decoding failed.

Cloning Objects - Handling Of Readonly Properties

Readonly properties of objects may now be modified in the __clone magic method of the class. Before, this this would have caused a fatal error.

class Foo 
{
    private readonly string $id;

    public function __construct()
    {
        $this->id = bin2hex(random_bytes(10));
    }

    public function __clone(): void 
    {
        // give the new object its own unique ID.
        $this->id = bin2hex(random_bytes(10));
    }
}

$foo = new Foo();
$foo2 = clone($foo);

The example above demonstrates the most common use case where I need this, whereby each object needs its own unique ID. E.g. cloning a database row etc.

Typed Class Constants

You can now set the type on a constant in a class:

class Foo
{
    const string BAR = 'foo'; 
} 

Anonymous Readonly Classes

Annonymous classes can now be readonly as demonstrated below:

$myClass = new readonly class {
    public function __construct(
        public string $foo = 'bar',
    ) {}
};
  • php.ini now supports fallback/default value syntax.
  • SQLite3: Default error mode set to exceptions.

Behaviour Change - Negative indices in arrays

The following code will change behaviour between 8.2 adn 8.3:

$array = [];
$array[-5] = 'a';
$array[] = 'b';
var_export($array);

In 8.3, the outputted array will show that the value b has an index of -4 because -4 is the next index after the first one at -5. However, in PHP 8.2 and before, the value b would have been at index 0. I have never noticed this before because I tend not to need to set negative indexes in arrays, not unless I'm manually setting all the indexes anyway. It is hard to think of legitimate circumstances where one is sometimes manually setting a numeric index, and sometimes not setting the index at all afterwards.

SQLite3 Exceptions By Default

SQLite3 default error mode is now set to exceptions like MySQL.

References

I didn't cover everything in this post, only the most important things that matter to me. Please read release notes in the reference list below for a more complete list of everything that changed.

Last updated: 3rd February 2024
First published: 3rd February 2024