Home >Backend Development >PHP Tutorial >Practical tips on how to optimize PHP function parameter passing

Practical tips on how to optimize PHP function parameter passing

伊谢尔伦
伊谢尔伦Original
2017-06-26 14:13:351399browse

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[&#39;title&#39;];  
        $name        = $a[&#39;name&#39;];  
        $value        = $a[&#39;value&#39;] ? $a[&#39;value&#39;] : "";  
        $type        = $a[&#39;type&#39;] ? $a[&#39;type&#39;] : "text";  
        $maxlength    = $a[&#39;maxlength&#39;] ? $a[&#39;maxlength&#39;] : "255";  
        $readonly    = $a[&#39;readonly&#39;] ? $a[&#39;readonly&#39;] : "";  
        $required    = $a[&#39;required&#39;] ? $a[&#39;required&#39;] : "false";  
        $check        = $a[&#39;check&#39;];  
        $id        = $a[&#39;id&#39;];  
        $width        = $a[&#39;width&#39;];  
        $tip        = $a[&#39;tip&#39;];  
    }  
    $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(  
        &#39;title&#39; = "编码",  
        &#39;name&#39; = "field0",  
        &#39;maxlength&#39; = 3,  
        &#39;required&#39; = "true",  
        &#39;width&#39; = 100,  
        &#39;tip&#39; = "提示:编号为必填项,只能填写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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn