Implicitly Marking Parameter as Nullable Is Deprecated: How to Fix It in PHP 8.4

Implicitly Marking Parameter as Nullable Is Deprecated: How to Fix It in PHP 8.4
Implicitly Marking Parameter as Nullable Is Deprecated: How to Fix It in PHP 8.4

PHP 8.4 fires this notice the moment a file loads, even for functions nobody calls. Here is what it means, the one-character fix, how to find every occurrence in a plugin without false positives, and the one case where adding the question mark gives you a second deprecation.

PHP 8.4 added a deprecation with a clunky name, “implicitly marking parameter as nullable is deprecated”, and you usually find out about it from a debug log that suddenly has a few hundred new lines in it:

Deprecated: dt_save_user(): Implicitly marking parameter $user as nullable is deprecated, the explicit nullable type must be used instead

This is the cheapest deprecation PHP has handed out in years. The fix is one character per parameter, nothing changes at runtime, and you can find every occurrence with a command you already have. One catch, though: in one kind of signature the obvious fix just swaps this notice for a different one.

What “implicitly marking parameter as nullable is deprecated” means

Since PHP 5.1 you could write a typed parameter with a null default, like WP_User $user = null, and PHP would quietly accept null for it even though the type says WP_User. The default made the type nullable behind your back. That is the “implicit” part.

PHP 8.4 deprecates that shortcut. You now have to say the parameter is nullable in the type itself. The notice always starts with the function name, and for methods it includes the class:

  • Function: dt_save_user(): Implicitly marking parameter $user as nullable is deprecated, the explicit nullable type must be used instead
  • Method: DT_Exporter::run(): Implicitly marking parameter $args as nullable is deprecated, the explicit nullable type must be used instead

That prefix is your first clue about whose code this is, which matters once you get to vendor libraries.

Why it fires for functions nobody calls

This one confuses people. The notice is raised when PHP compiles the file, not when the function runs. We loaded a file containing one function and one class method, called neither of them, and PHP 8.4 still printed both notices before the first line of output.

So if a plugin declares a function with this pattern anywhere in a file that gets included, the notice shows up the moment that file loads. It does not matter that the code path is dead. That also means “I never use that feature” is not a reason to ignore it.

The fix: one question mark

Here is the deprecated form, the kind of signature you will find in plenty of older WordPress plugins:

PHP
<?php
/**
 * Save profile data for a user, or for the current user when none is given.
 *
 * @param WP_User $user Optional. User to save. Default null (current user).
 * @return void
 */
function dt_save_user( WP_User $user = null ) {
	$user = $user ? $user : wp_get_current_user();
	// ... save logic.
}

And here is the fixed signature. Prefix the type with ? and update the PHPDoc so it tells the truth too:

PHP
<?php
/**
 * Save profile data for a user, or for the current user when none is given.
 *
 * @param WP_User|null $user Optional. User to save. Default null (current user).
 * @return void
 */
function dt_save_user( ?WP_User $user = null ) {
	$user = $user ? $user : wp_get_current_user();
	// ... save logic.
}

That is the whole fix for single types. You have three ways to write it, and they do not all run everywhere:

  • ?WP_User $user = null: works from PHP 7.1. Use this for single types. It is shorter and it runs on every PHP version WordPress still cares about.
  • WP_User|null $user = null: same meaning, but union syntax needs PHP 8.0. On PHP 7.4 it is a parse error, not a warning, so do not use it in a plugin that still supports 7.4.
  • A real default instead of null: if null was never a meaningful value, drop it. string $format = 'csv' is clearer than a nullable string you immediately replace.

Union types need the null spelled out

Union types trip it too. int|string $id = null raised the same notice in our run on 8.4. You cannot put ? in front of a union, so the fix is int|string|null $id = null.

What is exempt

Untyped parameters ($x = null) and mixed $x = null did not raise anything. mixed already includes null, so there is nothing implicit about it.

The trap: optional parameter before a required one

This is the case that bites people who fix these notices by search and replace. Older code sometimes put a = null parameter in front of a required one, like this:

PHP
<?php
// PHP 8.4: "Implicitly marking parameter $user as nullable is deprecated".
function dt_log_action_old( WP_User $user = null, string $context ) {}
// Adding "?" swaps it for a different notice, on PHP 8.1 and later:
// "Optional parameter $user declared before required parameter $context
//  is implicitly treated as a required parameter".
function dt_log_action_half( ?WP_User $user = null, string $context ) {}
// The real fix: drop the default. Since PHP 8.1 it does nothing anyway.
/**
 * Log an action against a user.
 *
 * @param WP_User|null $user    User the action belongs to, or null for none.
 * @param string       $context Where the action happened.
 * @return void
 */
function dt_log_action_fixed( ?WP_User $user, string $context ) {}

Why does the middle version complain? Because that = null was never a real default. A parameter in front of a required one cannot be skipped in a normal call, so callers always had to pass something. The old T $x = null form was a hack to get a nullable type before PHP 7.1 had ?. Once you write the ?, the hack is redundant and PHP tells you so.

Dropping the default changes nothing for existing callers on PHP 8.1 and later, since every call already has to pass the argument. It does make the signature honest.

Find every occurrence without false positives

You do not need a regex for this. Because the notice fires at compile time, php -l on PHP 8.4 finds every case for you. Linting compiles the file, and compiling is exactly when the deprecation is raised.

If you do not have PHP 8.4 installed locally, the official Docker image does the job. Run this from the plugin folder:

Bash
docker run --rm -v "$PWD":/p -w /p php:8.4-cli sh -c \
  "find . -name '*.php' -print0 | xargs -0 -n1 php -d error_reporting=-1 -d display_errors=stderr -l 2>&1 | grep -i nullable"

On a small test plugin this found all four real cases and nothing else. A regex grep for typed parameters followed by = null found the same four plus two false positives, both of them already-fixed |null unions. Regexes cannot tell int|string $id = null from int|string|null $id = null without getting ugly. The compiler can.

Bulk fixing

For a large codebase, two tools will rewrite the signatures for you:

  • Rector: the ExplicitNullableParamTypeRector rule in its PHP 8.4 set.
  • PHP-CS-Fixer: the nullable_type_declaration_for_default_null_value rule.

Both are fine. Neither is a reason to stop reading the diff. Check every change against the optional-before-required case above, and check that the output syntax matches the lowest PHP version you support.

Your code or a vendor library?

Read the function name at the start of the notice and the file path at the end. If the path is inside your own plugin, fix it. If it points into a vendor/ folder inside someone else’s plugin, it is a bundled library, often an older copy of something like Guzzle or a promise library.

Do not edit vendored code in place. The next plugin update overwrites your change, and you have now forked a library without meaning to. Update the plugin, or report it to the plugin author with the exact notice. If you maintain the plugin, update the dependency in composer.json instead of patching the copy.

If you are working through a full PHP 8 upgrade, our WordPress plugin PHP 8 compatibility guide covers the wider set of breaking changes, and PHP deprecation warnings explained covers how to log deprecations without showing them to visitors. For a refresher on how null itself behaves, see PHP declaring null variable.

FAQ

Does this deprecation break my site?

No. It is a notice, and behavior is unchanged on PHP 8.4 and later 8.x releases. It only becomes a problem when it is displayed on a live page, which means display_errors is on in production. Fix that setting regardless.

Do I need to fix it if my plugin still supports PHP 7.x?

Yes, and you can. ?Type works from PHP 7.1, so the fix is safe on every version from 7.1 up. Just avoid the Type|null spelling, which needs 8.0. If your floor is PHP 7.0, you cannot write the explicit form at all, and it is time to raise the floor.

Why does Guzzle or another library show this notice?

Because the copy bundled inside a plugin is older than the release where the library fixed its signatures, or comes from a major version that never got the fix. Guzzle 6 hit end of life in October 2023, and its last release still has these signatures. The fix belongs upstream. Update the plugin that ships it, or ask its author to bump the dependency.

Is ?string the same as string|null?

Yes. They mean the same thing. ?string is shorter and works from PHP 7.1. string|null needs 8.0. For unions, |null is the only option.

Will PHP 9 make this an error?

The RFC that introduced the deprecation says implicit nullable types are removed in PHP 9. PHP 9 has no announced release date, so there is no deadline to panic about. There is also no reason to wait.

Do not suppress this notice in CI. It is the one deprecation that costs you nothing to fix. One question mark per parameter. If that feels like too much work, the deprecation notice isn’t your real problem.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top