Req #46083 [Opn->Bgs]: PHP allows default parameters to appear before required parameters
| From: | johannes@php.net | Date: | Wed, 22 Dec 2010 12:57:28 +0000 |
| Subject: | Req #46083 [Opn->Bgs]: PHP allows default parameters to appear before required parameters | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-157240@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=46083&edit=1
ID: 46083
Updated by: johannes@php.net
Reported by: kevin dot benton at beatport dot com
Summary: PHP allows default parameters to appear before
required parameters
-Status: Open
+Status: Bogus
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: All
PHP Version: 5.2.6
Block user comment: N
Private report: N
New Comment:
This is an side effect of the possibility to allow NULL values with type
hinted parameters.
Previous Comments:
------------------------------------------------------------------------
[2008-09-15 17:07:53] kevin dot benton at beatport dot com
Description:
------------
PHP allows default parameters to appear before required parameters
From http://www.php.net/manual/en/functions.arguments.php,
It reports that...
<?php
function makeyogurt($type = "acidophilus", $flavour)
{
return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt("raspberry"); // won't work as expected
?>
will generate this output:
Warning: Missing argument 2 in call to makeyogurt() in
/usr/local/etc/httpd/htdocs/phptest/functest.html on line 41
Making a bowl of raspberry .
I think this is simply nasty behavior. If $type is defaulted and not
specified, PHP should know that it's a default parameter from the
parameter count and shift accordingly or generate a syntax error. A
default parameter that appears to the left of a required parameter isn't
optional with the way parameters are processed in PHP today.
I would rather have seen one of these results:
Error: makeyogurt() specifies required parameters after defaulted
parameters in /usr/local/etc/httpd/htdocs/phptest/functest.html on line
41.
or
Making a bowl of acidophilus raspberry.
While the second solution was rejected in http://bugs.php.net/24153, I
believe the first solution is still valid and quite frankly,
appropriate. If nothing else, this should be an option in php.ini to
prohibit optional parameters before required parameters.
C programmers are familiar with being able to do this by turning on
-Wall -Werror
Reproduce code:
---------------
See description
Expected result:
----------------
See description
Actual result:
--------------
See description
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=46083&edit=1