Req #70231 [Com]: Ability to set default params within a functions arguments as an array/object
| From: | rahi at atsspec dot co | Date: | Mon, 19 Oct 2015 22:28:19 +0000 |
| Subject: | Req #70231 [Com]: Ability to set default params within a functions arguments as an array/object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196692@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70231&edit=1
ID: 70231
Comment by: rahi at atsspec dot co
Reported by: mintystark at gmail dot com
Summary: Ability to set default params within a functions
arguments as an array/object
Status: Open
Type: Feature/Change Request
Package: Arrays related
Operating System: Ubuntu
PHP Version: 7.0.0beta3
Block user comment: N
Private report: N
New Comment:
I'm with jhdxr, this is a bad idea for the following reasons:
- array_merge already provides a way to set default values of an array
- such an implementation easily gets out of hand when the array elements contain non primitive data
types (objects)
- your use case is specific to array mashing, something the SPL collections/iterators are more
suited for
- if you're using the $arr as parameters/flags and not plain data consider wrapping it in a
class
Previous Comments:
------------------------------------------------------------------------
[2015-08-26 17:19:12] jhdxr@php.net
I think it's a bad idea to pass all the params in one single argument.
------------------------------------------------------------------------
[2015-08-10 16:58:53] mintystark at gmail dot com
Description:
------------
Currently if you want to send an array of parameters to a function, but have the function also use
defaults in the array you then need to use array_merge in the function. It would be useful if the
function arguments could handle this automatically.
As far as the syntax goes I don't care, but I would really like this as it would reduce the
amount of code I need to write and it would make it more readable. Plus allow for phpDocumentor and
other features to utilize this. Also I am sure you can make this more compatible with the compiler
thus ran faster then using other methods like array_merge() etc.
Suggested Syntax Options:
function do_stuff($arr = {'a' => 'blue', 'b' => 'red',
'c' => 'black', 'd' => 'yellow'})
function do_stuff($arr = ['a' => 'blue', 'b' => 'red',
'c' => 'black', 'd' => 'yellow'])
function do_stuff($arr = &array('a' => 'blue', 'b' =>
'red', 'c' => 'black', 'd' => 'yellow'))
Example Shown in the Test Script Below.
Test script:
---------------
function do_stuff($arr = {'a' => 'blue', 'b' => 'red',
'c' => 'black', 'd' => 'yellow'})
{
return $arr;
}
print_r(do_stuff(array('b' => 'green', 'c' => null,
'd' => ''));
Expected result:
----------------
// Should output
// Array('a' => 'blue', 'b' => 'green', 'c'
=> 'black', 'd' => '');
// OR
// Object('a' => 'blue', 'b' => 'green', 'c'
=> 'black', 'd' => '');
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70231&edit=1