What's new in PHP 8.3

  • avatar
  • 1.6K Views
  • 1 Like
  • 6 mins read

PHP 8.3 was released as scheduled on November 23, introducing numerous new features and improvements since the PHP 8.2 release. We'll go through most important features, performance improvements, changes and deprecations one by one.

Typed class constants

PHP 8.3 and subsequent versions now enable the declaration of types for PHP class constants. This guarantees type compatibility for constants when overridden by child classes and interface implementations. Before PHP 8.3, there was no way to enforce type compatibility programmatically.

Before PHP 8.3:

interface I {
// We may naively assume that the PHP constant is always a string.
const PHP = 'PHP 8.2';
}

class Foo implements I {
// But implementing classes may define it as an array.
const PHP = [];
}

With PHP 8.3:

interface I {
const string PHP = 'PHP 8.3';
}

class Foo implements I {
const string PHP = [];
}

// Fatal error: Cannot use array as value for class constant
// Foo::PHP of type string

Requiring types in class constants is essential to ensure that all derived classes, which override class constants, maintain the same type for those constants.

Deep-cloning of read-only properties

Within the magic __clone method, read-only properties can now be modified once, facilitating the deep-cloning of readonly properties.

Before PHP 8.3:

class PHP {
public string $version = '8.2';
}

readonly class Foo {
public function __construct(
public PHP $php
) {}

public function __clone(): void {
$this->php = clone $this->php;
}
}

$instance = new Foo(new PHP());
$cloned = clone $instance;

// Fatal error: Cannot modify readonly property Foo::$php

With PHP 8.3:

class PHP {
public string $version = '8.2';
}

readonly class Foo {
public function __construct(
public PHP $php
) {}

public function __clone(): void {
$this->php = clone $this->php;
}
}

$instance = new Foo(new PHP());
$cloned = clone $instance;

$cloned->php->version = '8.3';

New json_validate() function

In PHP 8.3, a new function has been introduced to validate JSON format. It returns true or false, indicating whether the provided string is a valid JSON string.

Before PHP 8.3:

function json_validate(string $string): bool {
json_decode($string);

return json_last_error() === JSON_ERROR_NONE;
}

var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

With PHP 8.3:

var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true

The function in PHP 8.3 employs the same underlying JSON parser as json_decode but is more efficient, consuming less memory and processing power. It solely analyzes the string without constructing any decoded value.

New #[\\Override] attribute

By adding the #[\\Override] attribute to a method, PHP indicates the requirement for a method with the same name to be present in either a parent class or an implemented interface. This attribute serves to explicitly indicate the intentional act of overriding a method from a parent class and facilitates refactoring by promptly detecting any removal of an overridden parent method.

Interfaces

interface ParentInterface {
public function myMethod();
}

interface ChildInterface extends ParentInterface {
#[\\Override]
public function myMethod();
}

Classes

class ParentClass {
public function myMethod() {}
}

class ChildClass extends ParentClass {
#[\\Override]
public function myMethod() {}
}

New Randomizer method

In PHP 8.3, the Random\\Randomizer class introduces a new getBytesFromString method. This method yields a random number sequence with a specified length (indicated by the $length parameter), composed exclusively of bytes from a designated set of bytes (specified by the $string parameter).

use Random\\Randomizer;

$randomizer = new Randomizer();

$randomString = $randomizer->getBytesFromString(
'abcdefghijklmnopqrstuvwxyz0123456789',
16,
);

echo $randomString;

Dynamic class constant fetch

In PHP 8.3, it's possible to retrieve class constants and enum objects using a variable name.

Before PHP 8.3:

class Foo {
const PHP = 'PHP 8.2';
}

$searchableConstant = 'PHP';

var_dump(constant(Foo::class . "::{$searchableConstant}"));

With PHP 8.3:

class Foo {
const PHP = 'PHP 8.3';
}

$searchableConstant = 'PHP';

var_dump(Foo::{$searchableConstant});

Similarly, in the case of Enums, it is now possible to dynamically retrieve an enum member.

enum Foo: string {
case PHP = 'PHP 8.3';
}

$searchableConstant = 'PHP';

var_dump(Foo::{$searchableConstant}->value);

There is no modification in behavior when trying to access an undefined class constant or an enum member. In both cases, an error for undefined constant occurs.

New SQLite exception class

In PHP 8.3, the SQLite3 extension incorporates a new exception class in the global namespace, extending the existing exception class.

class SQLite3Exception extends \\Exception {}

This addition does not introduce any extra constants, properties, or methods.


Conclusion

PHP 8.3 brings valuable enhancements, demonstrating the language's commitment to ongoing improvement and providing developers with useful features for efficient coding and maintenance.

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.