#46083 [NEW]: PHP allows default parameters to appear before required parameters

From: Date: Mon, 15 Sep 2008 17:07:54 +0000
Subject: #46083 [NEW]: PHP allows default parameters to appear before required parameters
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-128991@lists.php.net to get a copy of this message
From:             kevin dot benton at beatport dot com
Operating system: All
PHP version:      5.2.6
PHP Bug Type:     Feature/Change Request
Bug description:  PHP allows default parameters to appear before required parameters

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 bug report at http://bugs.php.net/?id=46083&edit=1
-- 
Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=46083&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=46083&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=46083&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=46083&r=fixedcvs
Fixed in release:             http://bugs.php.net/fix.php?id=46083&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=46083&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=46083&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=46083&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=46083&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=46083&r=notwrong
Not enough info:              http://bugs.php.net/fix.php?id=46083&r=notenoughinfo
Submitted twice:              http://bugs.php.net/fix.php?id=46083&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=46083&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=46083&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=46083&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=46083&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=46083&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=46083&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=46083&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=46083&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#128991) next »