Required parameter $xxx follows optional parameter $yyy
This style of function declaration has been deprecated in PHP 8.0. Writing functions like this has never made sense, since all parameters (up to the last required one) would need to be specified when the function was called. It also caused confusion with use of the ReflectionFunctionAbstract
class to analyze functions and methods.
The new deprecation simply ensures that function signatures follow the common sense assumption that required parameters, which must appear, should always be declared before optional ones.
The function should be rewritten to remove the default value on the earlier parameters. Since the function could never be called without declaring all parameters, this should have no effect on its functionality.
function test_function(int $var1, int $var2) {
return $var1 / $var2;
}
Deprecated: Required parameter $to_addr follows optional
In PHP 8, named parameters were added. This means that from now, parameters without a default value, are required to be BEFORE optional parameters.
An optional parameter is one with a default value: function example(string $optional = '');
. We call them optional, well, because they are optional, as you can call the function as follows: example();
.
Therefore, your prototype should change from:
public function send(string $to_name = '', string $to_addr, string $from_name, string $from_addr, string $subject = '', bool $reply_to = false): bool
to
public function send(string $to_addr, string $from_name, string $from_addr, string $to_name = '', string $subject = '', bool $reply_to = false): bool
You can read more about it here: https://www.php.net/manual/en/functions.arguments.php
required parameter $dompdf follows optional parameter $paper - barryvdh/laravel-dompdf
It probably happens because you are using php 8, and your current laravel dompdf doesn't support new changes of php 8.
To fix it try to use this line in your composer dependency:
"barryvdh/laravel-dompdf": "^0.9.0"
and run:
composer update
For more details see:
https://github.com/barryvdh/laravel-dompdf/issues/747
And you can see what happens in php-8 with function parameters in this issue:
Required parameter $xxx follows optional parameter $yyy
How to fix error: Deprecated: Required parameter
Your function has required paramaters after optional ones...
getAllFrom($field, $table, $where = NULL, $and = NULL, $orderfield, $ordering = "DESC")
$where
$and
are both optional (because they have default values). $orderfield
is required because it doesn't have a default value.
So the order should be:
getAllFrom($field, $table, $orderfield, $where = NULL, $and = NULL, $ordering = "DESC")
Order make $where
and $and
required too...
getAllFrom($field, $table, $where, $and, $orderfield, $ordering = "DESC")
If you need a "non-breaking" solution you could do something like...
function getAllFrom($field, $table, $where = NULL, $and = NULL, $orderfield = null, $ordering = "DESC") {
if ( $orderfield === null ) {
throw new InvalidArgumentException( 'Parameter 5 of getAllFrom function is required.' );
}
global $con;
$getAll = $con->prepare("SELECT $field FROM $table $where $and ORDER BY $orderfield $ordering");
$getAll->execute();
$all = $getAll->fetchAll();
return $all;
}
Required parameter $path follows optional parameter $handlers paypal sdk
Don't use the deprecated SDK, there is no support for it.
Ideally you also shouldn't use any redirects, as that is an old integration method, for old websites.
Here is the best way to proceed:
- Using the Checkout-PHP-SDK, create two routes on your server: one for 'Create Order' and one for 'Capture Order', documented here. These routes when accessed should return/output only JSON data (no other HTML or text)
- Pair your two routes with the following approval flow that calls them: https://developer.paypal.com/demo/checkout/#/pattern/server
php function's required parameter confusion
'Optional' arguments are just arguments with a default value, whether that value is null
, false
, a string (etc.) does not matter - if a function argument has a default value it is optional.
However, it wouldn't make sense to have an optional parameter come before a required parameter, as you must give the prior arguments some value (even if it's null
) in order to 'get to' the required argument - so all arguments before the last 'required' argument are effectively required.
Some examples:
// Bad
function bad($optional = 'literally anything', $required) {}
bad('required arg'); // This breaks - 'missing argument 2'
bad('something', 'required arg'); // This works - both parameters are needed
// Good
function($required, $optional = 'literally anything') {}
good('required arg'); // This works just fine, last argument has a default
good('required arg', 'something'); // Also works fine, overrides 'literally anything'
Update RE: Reflection
As noted above, putting an 'optional' parameter before a required parameter effectively makes that 'optional' parameter required, as a value must be given for it in order to ever satisfy the method signature.
For example, if the first argument has a default and second argument does not (like in the bad
function above), we NEED to pass a second argument, and so we NEED to pass a first argument also, so both are 'required'. If there are subsequent arguments with default values, still only the first 2 arguments are required.
Related Topics
How to Return Integer and Numeric Columns from MySQL as Integers and Numerics in PHP
Is Micro-Optimization Worth the Time
How to Create a Copy of an Object in PHP
How to Remove an Array Element in a Foreach Loop
Send Array With Ajax to PHP Script
How to Remove a Comma Off the End of a String
PHP Connection Failed: Sqlstate[Hy000] [2002] Connection Refused
MySQLi Query Returns Only One Row
How to Add a PHP Page to Wordpress
Rest API - Why Use Put Delete Post Get
PHP Login System: Remember Me (Persistent Cookie)
Convert a Comma-delimited String into Array of Integers
PHP Get All Subdirectories of a Given Directory
How to Properly Use a Pdo Object For a Parameterized Select Query