PHP 8.3: Unpacking the Latest Features for Developers at All Levels

Cleyton Bonamigo
3 min readNov 27, 2023

--

php 8.3 what's new in latest features

PHP, the ever-evolving server-side scripting language, has unveiled its latest version, PHP 8.3, packed with features that are sure to excite developers from beginners to veterans. Let’s dive into these new features, offering practical examples to illustrate their impact on your coding journey.

1. Typed Class Constants

Typed class constants are a game-changer for ensuring data integrity in your code. In earlier versions, the constant’s type was ambiguous, but PHP 8.3 introduces explicit typing for class constants.

// OLD Way
interface I {
const TEST = "Test"; // Could be a string, array, or even null
}

// PHP 8.3 Way:
interface I {
const string TEST = "Test"; // Explicitly a string
}

2. json_validate() Function

Validating JSON has never been more straightforward. Instead of relying on json_decode and handling exceptions, PHP 8.3 introduces json_validate().

// OLD Way
json_decode(json: '{"foo": "bar}', flags: JSON_THROW_ON_ERROR);


// PHP 8.3 Way
json_validate('{"framework": "Laravel"}'); // Returns true or false

3. Dynamic Class Constant Fetch

Fetching class constants dynamically adds flexibility to your code. Previously, you had to use the constant() function, but now, PHP 8.3 allows direct access.

// OLD Way
$name = 'NAME';
constant(Framework::class . '::' . $name); // Laravel

// PHP 8.3 Way
$name = 'NAME';
echo Framework::{$name}; // Direct access

4. #[Override] Attribute

The #[Override] attribute is a safety net, ensuring you're actually overriding a method from the parent class. It's a small but significant addition for maintainable OOP.

Example:

class ChildClass extends ParentClass {
#[Override]
public function someMethod() { /* ... */ }
}

5. Fallback Value for Environment Variables in INI Files

Simplifying configurations, PHP 8.3 allows for fallback values for INI settings, particularly useful in Docker environments.

Example:

[www]
listen = localhost:${DRUPAL_FPM_PORT:-9000}

6. Negative Array Indexes

PHP 8.3 introduces negative array indexes, adding more flexibility to array handling.

Example:

$array = [];
$array[-1] = "Hello";
echo $array[-1]; // Outputs 'Hello'

7. Enhancements to the Randomizer Class

The Randomizer class gets more powerful with methods like getBytesFromString() and getFloat(), offering more control over random data generation.

Example:

$randomizer = new \Random\Randomizer();
echo $randomizer->getBytesFromString('abc123', 5); // Random string from given characters

8. Refined DateTime Exceptions

PHP 8.3 addresses inconsistencies in date and time handling, introducing specific exceptions for common issues, thereby enhancing error handling and debugging.

Example:

try {
$date = new DateTime("invalid-date-string");
} catch (DateMalformedIntervalStringException $e) {
echo "Error: " . $e->getMessage();
}

PHP 8.3 is a testament to the language’s continued evolution, offering features that enhance code reliability, readability, and ease of maintenance. Whether you’re a beginner just getting your feet wet or a seasoned pro, these additions offer tools to make your PHP journey smoother and more efficient. Remember, keeping up-to-date with the latest version ensures you’re leveraging PHP to its full potential.

Happy coding!

--

--

Cleyton Bonamigo

A Senior Software Engineer, writing code in PHP/Laravel and passionate about new technologies.