Practical tips on how to optimize the transfer of PHP function parameters

Source: Internet
Author: User
When we write PHP code, we often need to make a number of changes to the code, such as changing the parameters repeatedly, so that our overall program performance is reduced, and increased a lot of work. We're going to introduce you today. The use of arrays for PHP function parameter passing method:

Let's look at a traditional custom function

/** * @Purpose: Insert Text field * @Method name:addinput () * @Parameter: str $title form Item title * @Parameter: Str $na Me element name * @Parameter: str $value Default value * @Parameter: str $type type, default text, optional password * @Parame TER:STR $maxlength Maximum input * @Parameter: str $readonly read-only * @Parameter: STR $required is required, default  Think False,true is required * @Parameter: str $check form verification function (JS) name * @Parameter: STR $id element ID, omitted without special need * @Parameter: int $width element width, unit: Pixel * @Parameter: str $tip element Tip * @Return: */Function ad Dinput ($title, $name, $value = "", $type = "Text", $maxlength = "255", $readonly, $required = "false", $check, $id, $width, $tip      {$this->form. = "<li>\n";      $this->form. = "<label>" $title. ": </label>\n"; $this->form. = "<input name=\" ". $name." \ "Value=\" ". $value." \ "Type=\" ". $type." \ "Maxlength=\" ". $maxlength." \ "Required=\" ". $required." \" Check=\ "". $check. " \ "Id=\" ". $id." \ "Class=\" input\ "". $readonly. "Style=\" width: ". $width." Px;\ "Showname=\" ". $title."      \ "/>"; $this->form. = "<span class=\" tip\ ">". $tip. "      </span>\n ";  $this->form. = "</li>\n"; }

The method of passing PHP function parameters is called

$form->addinput ("code", "Field0", "" "," Text ", 3," ");

In the beginning, only reserved $title, $name, $value, $type, $maxlength, $readonly and other parameters, after a period of use, found that these basic parameters can not meet the needs of the text box need to have JS verification, need to define CSS style, Need to add information, etc...

The addition of $required, $check, $id, $width, $tip and other parameters later found that all previous calls to this function need to be modified, adding a lot of work.

The invocation method of the PHP function parameter transfer method becomes

$form->addinput ("code", "Field0", "" "," Text ", 3," "," true "," "" "" "", "100", "Hint: the number is required and can only be filled in 3");

The following are the improved functions

function Addinput ($a) {if (Is_array ($a)) {$title = $a [' title '];          $name = $a [' name ']; $value = $a [' Value ']?          $a [' Value ']: ""; $type = $a [' type ']?          $a [' type ']: "text"; $maxlength = $a [' maxlength ']?          $a [' maxlength ']: "255"; $readonly = $a [' readonly ']?          $a [' ReadOnly ']: ""; $required = $a [' Required ']?          $a [' Required ']: "false";          $check = $a [' Check '];          $id = $a [' id '];          $width = $a [' width '];      $tip = $a [' Tip ']; $title, $name, $value = "", $type = "Text", $maxlength = "255", $readonly, $required = "false", $check, $id, $width, $tip $this-      >form. = "<li>\n";      $this->form. = "<label>" $title. ": </label>\n"; $this->form. = "<input name=\" ". $name." \ "Value=\" ". $value." \ "Type=\" ". $type." \ "Maxlength=\" ". $maxlength." \ "Required=\" ". $required." \ "Check=\" ". $check."              \"Id=\ "". $id. " \ "Class=\" input\ "". $readonly. "Style=\" width: ". $width." Px;\ "Showname=\" ". $title."      \ "/>"; $this->form. = "<span class=\" tip\ ">". $tip. "      </span>\n ";  $this->form. = "</li>\n"; }

The calling method becomes

$form->addinput (          ' title ' = ' encode ',          ' name ' = ' field0 ', '          maxlength ' = 3,          ' required ' = ' true ' ,          ' width ' =,          ' tip ' = ' Hint: The number is required, only 3 digits ', '      )  ;

Before and after the PHP function parameter passing method comparison can be found:

Traditional functions need to be expanded when the amount of change, when used must be in the order of the parameters to write, it is easy to error.

After the improved function extension can add new parameters at any time, only need to add the corresponding array key value at the call, each parameter is at a glance, no need to consider the order, code readability enhancement.

However, there are shortcomings in the method of PHP function parameter transfer, the amount of code increases, the programmer needs to write a lot of key values, there is the function of the judgment statement and ternary operation statement may affect the efficiency.

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.