Tips for Improving the method of passing PHP function parameters. After writing the code for repeated operations, we decided to improve the traditional PHP function parameter passing method and use arrays as parameters. See the following example. first, let's look at a traditional self-built system.After repeated operations, I decided to improve the parameter passing method of the traditional PHP function by using arrays as parameters. See the following example.
First look at a traditional user-defined function
- /**
- * @ Purpose: insert a text field
- * @ Method Name: addInput ()
- * @ Parameter: str $ title form item title
- * @ Parameter: str $ name Element name
- * @ Parameter: str $ value default value
- * @ Parameter: str $ type. the default value is text. password is optional.
- * @ Parameter: str $ maxlength maximum input
- * @ Parameter: str $ readonly read-only
- * @ Parameter: str $ required: required. the default value is false. true indicates required.
- * @ Parameter: str $ check form verification function (js) name
- * @ Parameter: str $ id element id, which is omitted unless otherwise required.
- * @ Parameter: int $ width the width of the element. unit: Pixel.
- * @ Parameter: str $ tip element prompt information
- * @ Return:
- */
- Function addInput ($ title, $ name, $Value="", $Type="Text", $Maxlength="255",
$ Readonly, $Required="False", $ Check, $ id, $ width, $ tip)
- {
- $ This->Form.="
- N";
- $ This->Form.="". $ Title .": Label>N ";
- $ This->Form.="<InputName ="". $ Name .""Value= "". $ Value .""Type= ""
. $ Type .""Maxlength= "". $ Maxlength .""Required= "". $ Required .""Check= ""
. $ Check .""Id= "". $ Id .""Class= "Input" ". $ readonly ."Style= "Width:". $ width.
"Px ;"ShowName= "". $ Title .""/>";
- $ This->Form.="<SpanClass ="Tip">". $ Tip ." Span>N ";
- $ This->Form.="
N"
;
- }
This is a function that inserts a text box in the form class I wrote.
The call method for passing PHP function parameters is
- $ Form->AddInput ("encoding", "field0", "", "text", 3 ,"");
At the beginning, only $ title, $ name, $ value, $ type, $ maxlength, $ readonly and other parameters are reserved, it is found that these basic parameters cannot meet the requirements. the text box requires JavaScript verification, CSS styles need to be defined, and prompt information needs to be added...
After adding parameters such as $ required, $ check, $ id, $ width, and $ tip, it is found that all previously called functions need to be modified, which adds a lot of work.
PHP function parameter transfer method call method becomes
- $ Form->AddInput ("encoding", "field0", "", "text", 3, "", "true ",""
, "", 100, "prompt: The number is required. only three digits are allowed ");
If this function is used in many places, it takes a long time to change it one by one.
The following are my 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 .= "
- n";
- $this->form .= "".$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 .= "
n"
;
- }
The call method is changed
- $ Form->AddInput (
- Array (
- 'Title' = "encoding ",
- 'Name' = "field0 ",
- 'Maxlength' = 3,
- 'Required' = "true ",
- & Apos; width & apos; = 100,
- 'Tip '= "prompt: The number is required. only three digits are allowed ",
- )
- );
After comparing the passing methods of PHP function parameters, we can find that:
A traditional function requires a large amount of changes when it needs to be extended. when used, it must be written in the order of parameters, which is prone to errors.
New parameters can be added at any time when the improved function is extended. you only need to add the corresponding array key value during the call. each parameter is clear at a glance, so you do not need to consider the order and the code readability is enhanced.
However, there are still some shortcomings in the PHP function parameter passing method. as the amount of code increases, programmers need to write a lot more key values. In addition, the function may affect the efficiency of judgment statements and trielement operation statements.
After repeated operations, I decided to improve the traditional PHP function parameter passing method and use arrays as parameters. please refer to the following example. first look at a traditional self...