php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53399 Add E_STRICT when defining a required funcparameter after an optional parameter
Submitted: 2010-11-24 16:25 UTC Modified: 2020-03-03 11:02 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: jbafford at zort dot net Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: * OS:
Private report: No CVE-ID: None
 [2010-11-24 16:25 UTC] jbafford at zort dot net
Description:
------------
PHP does not emit a warning 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.

The attached patch includes the following changes:

* add an E_STRICT warning when such a function is defined. (The warning will 
only 
appear when E_STRICT is included in the php.ini, since it happens at script 
compile time, before error_reporting can be called.)

* add Zend/tests/func_optarg1.phpt and Zend/tests/func_optarg2.phpt to test for 
the presence and absence of the warning

* Fixes Zend/tests/call_user_func_005.phpt, which fails with the warning added.


Patches

strict-required-opt2.patch (last revision 2010-11-25 01:27 UTC by jbafford at zort dot net)
strict-required-opt.patch (last revision 2010-11-24 15:27 UTC by jbafford at zort dot net)

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-01 15:19 UTC] jani@php.net
-Operating System: all +Operating System: -PHP Version: trunk-SVN-2010-11-24 (SVN) +PHP Version: *
 [2011-10-16 00:20 UTC] stas@php.net
I think the idea is good, however the patch seems too complicated. I do not think 
we need additional flag and not sure why would we need additional pass through 
arguments to do it - if we say everything after certain arg is optional, then we 
already know how to do it, we have settings for that in args structure. And we can 
detect this stuation immediately when we parse the arguments on compile.
 [2017-03-27 20:53 UTC] tpunt@php.net
Related to bug #68445
 [2020-01-08 16:04 UTC] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2020-01-13 15:36 UTC] carusogabriel@php.net
The following pull request has been associated:

Patch Name: Deprecate required param after optional
On GitHub:  https://github.com/php/php-src/pull/5067
Patch:      https://github.com/php/php-src/pull/5067.patch
 [2020-03-03 11:02 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2020-03-03 11:02 UTC] nikic@php.net
This throws a deprecation warnings as of PHP 8.
 [2020-12-29 12:40 UTC] ron dot kirschler at gmail dot com
Some applications make (sometimes heavy) use of autowiring and the related feature of setting parameters by name rather than by order. Here it is quite common to set parameters in a different, because semantically different meaningful order.

For me, there are more advantages to ignoring this deprecation notice rather than following it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 16:01:29 2024 UTC