Deprecated: Required parameter $x follows optional parameter $y Error in PHP 8.1

PHP Errors No Comments

After upgrading our servers to PHP 8.1, we received a warning as

Deprecated: Required parameter $x follows optional parameter $y

This was the code that has this error:

public function function_name ($y='', $x) {
function code....
}

To fix this, we changed the above code to:

public function function_name ($y, $x) {
        $y=$y ?? '';
}

That’s it.

We had the same error in one of the constructors:

public function __construct($x, $y = 12, $z = '', $k){
function code....
}

In order to fix this, we just need to place the z variable at the end of variables as below:

public function __construct($x, $y = 12, $k, $z = ''){ 
function code.... 
}

That’s it.

Hope this helps!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments