Breaking Down PHP 8.3: Deep Dive Into PHP’s Latest Features

Martin Kolar
3 min readMar 18, 2024

Exploring the latest version of PHP and all of its must see features.

In this article we will be uncovering new features introduced in the latest version of PHP (8.3) as well as diving deeper into some real world examples using these new features which will be hopefully useful to you :)

Typed class constants

In PHP 8.3 You can now typehint class constants

class AuthenticationHandler {
public const int MAX_ATTEMPTS = 5;
}

This might seem as a small and perhaps irrelevant upgrade but as somebody who works a lot with strongly typed languages I love this new addition.

New json_validate function

This new addition will be quite useful for every PHP dev but especially for those who have to deal on a regular basis with API responses. In previous version of PHP in order to validate whether a string was a valid JSON or not, we had to decode the string and check if any errors were thrown, this new function takes care of that and in a much more efficient way because it uses less memory compared to decoding the string.

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

New Override attribute

PHP 8.3 introduces a new #[Override] attribute which serves to show a programmer’s specific intent. In other words, it can be used to explicitly indicate that a method in a child class is intended to override a method in its parent class.

class Parent {
public function hello() {
echo "Hello from ParentClass!";
}
}

class Child extends Parent {
#[Override]
public function hello() {
echo "Hello from ChildClass!";
}
}

Readonly modifications

This new RFC deals with a specific use case, overwriting property values within the __clone magic method in order to allow deep cloning read only properties. Here’s an example with this new addition:

readonly class Configuration {
public function __construct(
public Logger $logger,
) {}

public function __clone()
{
// This will work, even though `logger` is a readonly property.
$this->logger = new Logger();
}
}

Dynamic fetching of class constants

PHP 8.3 allows us to fetch constants with a more dynamic syntax, we can now use variables to fetch constants like this:

class UserRole {
public const ADMIN = 'admin';
public const MANAGER = 'manager';
public const GUEST = 'guest';
}

$userRole = 'ADMIN';

echo UserRole::{$userRole}; // Output: "admin"

Negative array indexes

Last but not least, PHP 8.3 now introduces negative array indexes, which makes array handling more powerfull.

$array = [];
$array[-1] = "Foo";

echo $array[-1]; // Output: 'Foo'

I hope this article has been helpful and usefull to you. Happy coding :)

Enjoyed this article?

🔔 Then don’t forget to follow me to make sure you don’t miss out on my new posts.

You can also support me and my work on this platform with a coffee ☕

Buy Me a Coffee

Thank you for your support and encouragement! Happy reading! 🙂

--

--

Martin Kolar

Hey There 👋 I'm Martin, a Full Stack Web Developer @AboutYou with a passion for sharing knowledge and best practices in web development.