← Back to Articles
March 17, 2026

PHP 8.4: Resolving Nullable Type Deprecations

PHP 8.4 Nullable Types

If you're upgrading to PHP 8.4 for Magento or another framework, you might run into this error when you compile your modules:

Deprecated Functionality: MageWorx\ShippingRules\Model\ResourceModel\Carrier\Grid\Collection::__construct(): Implicitly marking parameter $connection as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/app/code/MageWorx/ShippingRules/Model/ResourceModel/Carrier/Grid/Collection.php on line 53

This guide will help you resolve the current error and prepare you for a seamless PHP upgrade by preventing common issues. Let's get started with what you need to know about PHP 8.4, Nullable Type.

Nullable Type

PHP 8.4 is set to introduce a change that will strengthen the language's type system by deprecating implicitly nullable types. This move continues PHP's evolution towards stricter, more predictable code. While this change is a positive step forward, it means a common coding pattern will now throw a deprecation notice.

Let's dive into what's changing, why it's happening, and how you can prepare your code. 🧐

The Deprecation: Implicit vs. Explicit Nullability

In older versions of PHP, it was possible to declare a typed property or parameter and assign null as its default value without explicitly marking the type as nullable. This made the type implicitly nullable.

The Old Way (Now Deprecated): This code will trigger a deprecation notice in PHP 8.4 and will likely become a fatal error in PHP 9.0.
class User {
    // Deprecation: Type 'string' is not nullable, but default value is null
    public string $name = null;
}

The problem here is the type declaration string does not inherently allow null. However, by setting the default value to null, PHP previously allowed it. This creates a contradiction in the code: the type says one thing, but the default value says another.

The Correct Way (PHP 8.4+): To fix this, you must explicitly mark the type as nullable using the ? notation.
class User {
    // Correct: The '?' makes the type explicitly nullable
    public ?string $name = null;
}

By adding the question mark (?string), you are clearly stating that the $name property can be either a string or null. This makes your code more readable and your intentions unambiguous.

How to Prepare Your Codebase

Updating your code for this change is straightforward. You need to find all instances where a typed property, parameter, or return type has a default value of null but isn't explicitly marked as nullable.

1. Find the Problematic Code

You can search your project for patterns like:

2. Apply the Fix

For each instance you find, simply add a question mark (?) before the type declaration.

Before:

function createUser(string $username = null) {
    // ...
}

After:

function createUser(?string $username = null) {
    // ...
}

Conclusion

Adhering to best practices like writing clean code, following PSRs, and continuously staying updated with your language's features is crucial. This discipline minimizes rework and significantly simplifies long-term maintenance. I hope you find this advice valuable in your professional and academic endeavors. Best regards.