Req #70231 [Com]: Ability to set default params within a functions arguments as an array/object
Edit report at https://bugs.php.net/bug.php?id=70231&edit=1
ID: 70231
Comment by: jhdxr@php.net
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 think it's a bad idea to pass all the params in one single argument.
Previous Comments:
------------------------------------------------------------------------
[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
Thread (3 messages)