For example, there is a function () {code ...} now I only need to use the first and last parameters, which are usually find (& #039; xxx & #039;, & #039; & #039;, & #039; & #039;, & #039; & #039 ;,..., & #039; xxx & #039;) Is there any elegant style? For example, a function ()
Function find ($ conditions = null, $ sort = null,... (many parameters)..., $ fields = null, $ limit = null)
Now I only need to use the first and last parameters, which are usually find ('xxx ','','','',..., 'xxx') It looks pretty bad. Is there any elegant writing?
Reply content:
For example, a function ()
Function find ($ conditions = null, $ sort = null,... (many parameters)..., $ fields = null, $ limit = null)
Now I only need to use the first and last parameters, which are usually find ('xxx ','','','',..., 'xxx') It looks pretty bad. Is there any elegant writing?
This issue is discussed in two cases.
- If you want to fix some of the values
- If you want some of the parameters to have default values
Case 1: If you want to fix the values of some of these parameters in advance
You can evaluate the function partially (colized) to obtain a new function. You can use the new function to operate it later.
You can customize the values of some of the parameters when you are using colized.
For example
// Original function find ($ conditions = null, $ sort = null ,.. (Many parameters ).., $ fields = null, $ limit = null) // function curryFind ($ conditions, $ limit) {return find ($ conditions, "sort ",..., "fields", $ limit );}
Case 2: If you want some of the parameters to have default values
Optional parameters can be combined into an array. Parameters are input as arrays and merged with the default parameter array.
For example
function find($options) { $defaultOptions = array( 'conditions' => 'c', 'sort' => 's', ... 'fields' => 'f', 'limit' => 'l' ); $options = array_merge($defaultOptions, $options); // do something.}
There is no obvious difference between the two cases, but only the method for processing multiple parameters. Use it as needed.
References
- Http://stackoverflow.com/questions/1609985/is-it-possible-to-curry-method-calls-in-php
- Colihua: http://en.wikipedia.org/wiki/Currying
If a function has a bunch of parameters, it indicates that there is a problem with the function design. Consider refactoring as soon as possible.
If there is no way, you can consider usingcall_user_func_array()
Function to construct an array and pass it in.
Bringing an array...
function find( array( 'conditions' => '1', 'sort' => '2', // .... 'limit' => '3' ) );
Don't write if you don't have one...
You can get the last parameter of this method to the second parameter.
Function find ($ conditions = null, $ limit = null, $ sort = null,... (many parameters)..., $ fields = null)
function abc($str) { parse_str($str,$arr); $arr['key'];}$some = abc('key=1&p=2&some......');
// Use a non-fixed function static function fun () {$ numargs = funcNumArgs (); // obtain the number of parameters; $ argList = funcGet_args (); // get the parameter list} // design the function by yourself. Just design a function in s to bring it to the old function.
FuncGetArgs and funcGetNums Functions
Why is the underline gone?