In php, functions are used. If there are many parameters and only the last parameter is used, what is the elegant method?

Source: Internet
Author: User
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.

  1. If you want to fix some of the values
  2. 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

  1. Http://stackoverflow.com/questions/1609985/is-it-possible-to-curry-method-calls-in-php
  2. 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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.