Php optional parameter is provided before required

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8 Deprecated function: Optional parameter $input declared before required parameter $form_state is implicitly treated as a required parameter in include_once() #6005

PHP 8 Deprecated function: Optional parameter $input declared before required parameter $form_state is implicitly treated as a required parameter in include_once() #6005

Comments

Description of the bug

I’m running a backdrop site on php 8.1 with deprecation warnings enabled, and I’m seeing a handful of these:

Deprecated function: Optional parameter $input declared before required parameter $form_state is implicitly treated as a required parameter in include_once() (line 1509 of backdrop/docroot/core/includes/bootstrap.inc). 

These functions called via bootstrap.inc can be hard to track down.
Searching for $input before $form_state in function definitions returns a set of Form API value callbacks, all from core:

$value_callback($element, $input, $form_state); 
  • file_field_widget_value()
  • file_managed_file_value()
  • form_type_image_button_value()
  • form_type_textarea_value()
  • form_type_textfield_value()
  • list_boolean_allowed_values_callback()
Читайте также:  Gui module for python

Should we check all these to make sure that there are no optional items before required?

Steps To Reproduce

To reproduce the behavior:

Actual behavior

PHP warnings about deprecations

Expected behavior

Additional information

Add any other information that could help, such as:

The text was updated successfully, but these errors were encountered:

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional parameters before required parameters #1827

Optional parameters before required parameters #1827

Comments

Optional parameters (with default values) always should be at the end, otherwise they are not optional 😄

See http://php.net/manual/en/functions.arguments.php
«Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected.»

The following is a list I found, where an required parameter appears after optional parameters:

Dompdf\Canvas function __construct($paper = "letter", $orientation = "portrait", Dompdf $dompdf); Dompdf\Adapter\CPDF public function __construct($paper = "letter", $orientation = "portrait", Dompdf $dompdf) Dompdf\Adapter\GD public function __construct($size = 'letter', $orientation = "portrait", Dompdf $dompdf, $aa_factor = 1.0, $bg_color = array(1, 1, 1, 0)) Dompdf\Adapter\PDFLib public function __construct($paper = "letter", $orientation = "portrait", Dompdf $dompdf) Dompdf\Renderer\AbstractRenderer protected function _border_line($x, $y, $length, $color, $widths, $side, $corner_style = "bevel", $pattern_name, $r1 = 0, $r2 = 0) Cpdf function addImagePng($file, $x, $y, $w = 0.0, $h = 0.0, &$img, $is_mask = false, $mask = null) function addPngFromBuf($file, $x, $y, $w = 0.0, $h = 0.0, &$data, $is_mask = false, $mask = null) private function addJpegImage_common(&$data, $x, $y, $w = 0, $h = 0, $imageWidth, $imageHeight, $channels = 3, $imgname) 

Changing the order breaks backwards-compatibility. Not sure what to do here.

The text was updated successfully, but these errors were encountered:

Источник

PHP 8.0: Deprecate required parameters after optional parameters in function/method signatures

When declaring a function or a method, adding a required parameter after optional parameters is deprecated since PHP 8.0.

This means the following function signature triggers a deprecation notice:

function foo($param_optional = null, $param_required) < // ^^ optional parameter , ^^ required parameter >
Deprecated: Required parameter $param_required follows optional parameter $param_optional in . on line . 

If you have a require parameter (i.e. a parameter without a default value set in its signature) after an optional one (i.e. a parameter with a default value), it makes all parameters before it essentially required because the caller has to explicitly pass a value for optional parameters as well.

PHP does not emit a notice of any kind when defining a function with a required
parameter after an optional parameter. For example:
function foo($optional = 1, $required) <>

It doesn’t make sense to define a required parameter after an optional one,
since that effectively makes all preceding optional parameters required. Since this is
an error that can produce bugs and other warnings if one is not careful (calling
the above function with less than two parameters will cause warnings to be
emitted for the missing $required parameter), PHP should emit a warning of some kind
when functions like this are defined.

PHP documentation already explains that having required parameters after optional parameters is incorrect. There was no deprecation notice until PHP 8.0, even though it hints a likely issue in the code architecture.

Nullable parameters

If you have used typed parameters with a default value set to null , you can use nullable types instead. To provide an example:

function foo(string $param_optional = null, $param_required) < // ^^ poormans nullable param , ^^ required parameter >

This «trick» was used with PHP 7.0, there was no nullable type support until PHP 7.1. The snippet above can be replaced with the following:

function foo(?string $param_optional, $param_required) < // ^^ optional parameter , ^^ required parameter >

The deprecation does not apply if there is a type declared on the optional parameter, and the default value is null .

The following will not trigger a deprecation notice:

function foo(string $param_optional = null, $param_required) <>

This is because there is a type ( string ) declared for the optional $param_optional parameter, and its default value is null . Setting any other default value (such as $param_optional = ‘bar’ ), or not declaring the type will trigger the deprecation notice.

Thanks to M1keSkydive and the_alias_of_andrea for the insight to extend this part.

Backwards compatibility impact

If you have any functions/methods that have required parameters after optional parameters, you will get a deprecation notice similar to the one in the example. It is most likely an indication of poorly defined architecture. The deprecation notice is triggered at the compile time, and it will be raised even if it is not called.

You can work around it by removing the default value in all optional parameters before the last required parameter. This should not break your existing code because all callers must pass a value to the optional parameters before proving values for the required parameters that appear later.

© 2018-2023 PHP.Watch, with ❤ from Ayesh • About PHP.Watch

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

null default-value promoted class parameter before required parameter is silently ignored #11488

null default-value promoted class parameter before required parameter is silently ignored #11488

Comments

Description

 class Foobar < public function __construct( public string|null $a = null, public $b, ) <> > new Foobar(b: 'x');
PHP Fatal error: Uncaught ArgumentCountError: Foobar::__construct(): Argument #1 ($a) not passed 

But I expected this output instead:

PHP Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter PHP Fatal error: Uncaught ArgumentCountError: Foobar::__construct(): Argument #1 ($a) not passed 

To explain; $a does not end up being optional, and if you perform reflection on the parameter and/or property, neither parameter/property has a default-value. So PHP is silently ignoring the user-specified behavior, which only surfaces once you do the call to the function (or as it is sometimes in our case, it doesn’t show up at all because some objects are created via reflection).

Further information

Normal functions and methods support «poor mans nullable types» for now, which makes the above case a weird quirk of the backwards compatibility, as noted by @KapitanOczywisty in this post #11485 (comment).

However, constructors with parameter/property promotion behave in a stricter manner.

class C1 < public function __construct( public string|null $a = null, public $b, ) <> > class C2 < public function __construct( public string|null $a pl-s">a", public $b, ) <> > class C3 < public function __construct( public string $a = null, public $b, ) <> > class C4 < public function __construct( public string $a pl-s">a", public $b, ) <> > class C5 < public function __construct( public $a = null, public $b, ) <> >
  • C1 emits no error, but silently makes the parameter/property mandatory.
  • C2 emits «Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter».
  • C3 emits «Fatal error: Cannot use null as default value for parameter $a of type string».
  • C4 emits «Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter».
  • C5 emits «Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter».

C3 proves that poor mans nullable types aren’t intended to apply to constructors with typed promoted parameters/properties like they do for normal parameters, and C5 proves that it’s deprecated for them regardless. And C2 and C4 proves that optional parameters aren’t allowed before required parameters and emits a deprecation.

However, C1 emits no warning at all, even though parameter/property doesn’t end up having a null default-value, nor is it optional. So C1 behaves identically to not specifying a default-value at all, whereas the only purpose of specifying the null default-value is to make it optional, which it isn’t.

So it definitely seems like C1 should emit a deprecation as well.

Источник

Оцените статью