Ffile2fix
Sign in Get started

How to fix "Optional parameter declared before required" in PHP

Deprecated: Optional parameter $limit declared before required parameter $type is implicitly treated as a required parameter in /home/user/public_html/wp-content/plugins/my-plugin/includes/query.php on line 12

A function has a parameter with a default value followed by one without a default, like function get($limit = 10, $type). Because callers must pass $type, they must also pass $limit, so the default is useless. PHP 8 warns about this, and PHP 8.1+ uses the current wording.

Also appears as: Deprecated: Required parameter $b follows optional parameter $a (PHP 8.0 wording) · PHP Deprecated: Optional parameter $args declared before required parameter $callback is implicitly treated as a required parameter · Deprecated: Optional parameter $options declared before required parameter $name is implicitly treated as a required parameter in /home/user/app/src/Service.php

Common causes

  • Parameters were reordered or a new required parameter was added at the end
  • Old code written for PHP 5 or 7, where this was silently allowed
  • Copying signatures from older WordPress or framework code
  • Using = null for a nullable type instead of the ?Type syntax

How to fix it

  1. Open the function in the message. Go to the file and line shown. Note which optional parameter appears before which required one.
  2. Move required parameters first. Reorder the signature so all required parameters come before optional ones, for example function get($type, $limit = 10). Update every call to match the new order.
  3. Or remove the useless default. If you cannot change call sites safely, delete the default value, for example function get($limit, $type). This does not change behaviour because the default was never used.
  4. Use ?Type for nullable parameters. Write ?Foo $foo instead of Foo $foo = null when the parameter is required but may be null. Note that PHP 8.4 also deprecates implicit nullable types (Foo $foo = null), so ?Foo is the safe form.
  5. Update third-party code. If the file is inside a plugin, theme or vendor folder, update that package rather than editing it, so your fix is not lost on the next update.

Before and after reordering parameters

// Deprecated
function fetchPosts($limit = 10, $type) { }

// Fixed: required first
function fetchPosts($type, $limit = 10) { }

// Required but nullable
function send(?Logger $logger, string $msg) { }

How to stop it happening again

  • Always put required parameters before optional ones
  • Use named arguments in PHP 8 instead of long optional lists
  • Test with deprecation notices visible before upgrading PHP
  • Run a PHP compatibility scan on legacy code

Frequently asked questions

Does this break anything?

No. It is a deprecation notice and the code runs as before. The default value was already ignored.

Is $param = null before a required parameter allowed?

In PHP 8.0 a typed parameter with = null was exempt from this notice, but later PHP 8.x releases warn about it too. Use ?Type $param without a default instead.

Why does it say "implicitly treated as a required parameter"?

PHP is telling you the default can never be used, because you cannot skip it when a later required argument must be passed.