When we write PHP code, we often need to make multiple upgrades and changes to the code. Such constant and repeated modification of parameters will reduce the performance of our entire program and increase a lot of workload. . Today we will introduce to you how to use arrays to pass PHP function parameters:
Let’s first look at a traditional custom function
/** * @Purpose: 插入文本域 * @Method Name: addInput() * @Parameter: str $title 表单项标题 * @Parameter: str $name 元素名称 * @Parameter: str $value 默认值 * @Parameter: str $type 类型,默认为text,可选password * @Parameter: str $maxlength 最长输入 * @Parameter: str $readonly 只读 * @Parameter: str $required 是否必填,默认为false,true为必填 * @Parameter: str $check 表单验证function(js)名称 * @Parameter: str $id 元素id,无特殊需要时省略 * @Parameter: int $width 元素宽度,单位:象素 * @Parameter: str $tip 元素提示信息 * @Return: */ function addInput($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 of the PHP function parameter passing method is
$form->addInput("编码","field0","","text",3,"");
. At the beginning, only $title, $name, $value, $type, $maxlength, $readonly and other parameters are reserved. After a period of use, It was found that these basic parameters cannot meet the needs. The text box needs js verification, CSS style needs to be defined, prompt information needs to be added, etc...
Added $required, $check, $id, $width, $tip After waiting for the parameters, I found that all previous calls to this function needed to be modified, which added a lot of workload.
The calling method of the PHP function parameter passing method became
$form->addInput("编码","field0","","text",3,"","true","" ,"",100,"提示:编号为必填项,只能填写3位");
The following is the improved function
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( array( 'title' = "编码", 'name' = "field0", 'maxlength' = 3, 'required' = "true", 'width' = 100, 'tip' = "提示:编号为必填项,只能填写3位", ) );
After comparing the PHP function parameter passing methods before and after, it can be found that:
The traditional function changes a lot when it needs to be expanded. It is easy to make mistakes when writing in the order of parameters.
When the improved function is expanded, new parameters can be added at any time. You only need to add the corresponding array key value when calling. Each parameter is clear at a glance, no need to Considering the order, the readability of the code is enhanced.
However, the improvement of the PHP function parameter passing method still has shortcomings. The amount of code has increased, requiring programmers to write a lot more key values, and there is also the ## in the function. #Judgment statements and ternary operations statements may affect efficiency.
The above is the detailed content of Practical tips on how to optimize PHP function parameter passing. For more information, please follow other related articles on the PHP Chinese website!