Bug #68445 [Opn]: Optional arguments may precede required arguments in function declarations
| From: | dmitry@php.net | Date: | Thu, 14 Jul 2016 11:08:27 +0000 |
| Subject: | Bug #68445 [Opn]: Optional arguments may precede required arguments in function declarations | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-202310@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68445&edit=1
ID: 68445
Updated by: dmitry@php.net
Reported by: ajf at ajf dot me
Summary: Optional arguments may precede required arguments in
function declarations
Status: Open
Type: Bug
Package: Scripting Engine problem
Operating System: OS X Yosemite
PHP Version: 5.6.3
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
I think, declaration of arguments without default values after the ones with default values - should
be prohibited by compiler (at least since PHP-7.1)
Previous Comments:
------------------------------------------------------------------------
[2014-11-18 21:31:42] ajf at ajf dot me
That's a fair point. This would change if nullable typehints (as proposed by Levi's RFC:
https://wiki.php.net/rfc/nullable_typehints)
were added.
------------------------------------------------------------------------
[2014-11-18 20:08:50] mike@php.net
Mind that this is also the only way to declare an argument with a nullable class hint.
------------------------------------------------------------------------
[2014-11-18 19:47:12] ajf at ajf dot me
Description:
------------
Optional arguments can be specified before required arguments, which would suggest that you can omit
them. Yet, if you actually call the function, it will complain that you didn't pass the
required arguments.
The fix would be either to support optional arguments before required arguments at call-time, or to
error when this is done in a function declaration.
Test script:
---------------
<?php
function foo($a = NULL, $b) {
var_dump($b);
}
foo(2);
Expected result:
----------------
int(2)
Actual result:
--------------
Warning: Missing argument 2 for foo(), called in %s on line 7 and defined in /in/b1HC3 on line 3
Notice: Undefined variable: b in %s on line 4
NULL
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68445&edit=1