Rumah > Artikel > pembangunan bahagian belakang > PHP函数参数传递方法的具体改进技巧分享_PHP教程
当我们在写本人在经历了多次重复操作之后决定改进一下传统PHP函数参数传递方法,使用数组作为参数,请看下面的例子.
先看一个传统的自定义函数
<ol class="dp-xml"> <li class="alt"><span><span>/** </span></span></li> <li class=""><span>* @Purpose: 插入文本域 </span></li> <li class="alt"><span>* @Method Name: addInput() </span></li> <li class=""><span>* @Parameter: str $title 表单项标题 </span></li> <li class="alt"><span>* @Parameter: str $name 元素名称 </span></li> <li class=""><span>* @Parameter: str $value 默认值 </span></li> <li class="alt"><span>* @Parameter: str $type 类型,默认为text,可选password </span></li> <li class=""><span>* @Parameter: str $maxlength 最长输入 </span></li> <li class="alt"><span>* @Parameter: str $readonly 只读 </span></li> <li class=""><span>* @Parameter: str $required 是否必填,默认为false,true为必填 </span></li> <li class="alt"><span>* @Parameter: str $check 表单验证function(js)名称 </span></li> <li class=""><span>* @Parameter: str $id 元素id,无特殊需要时省略 </span></li> <li class="alt"><span>* @Parameter: int $width 元素宽度,单位:象素 </span></li> <li class=""><span>* @Parameter: str $tip 元素提示信息 </span></li> <li class="alt"><span>* @Return: </span></li> <li class=""><span>*/ </span></li> <li class="alt"> <span>function addInput($title,$name,$</span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">""</font></span><span>,$</span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span>,$</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"255"</font></span><span>,<br>$readonly,$</span><span class="attribute"><font color="#ff0000">required</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"false"</font></span><span>,$check,$id,$width,$tip) </span> </li> <li class=""><span>{ </span></li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<li>n"</li></font></span><span>; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<label>"</label></font></span><span>.$title.":</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">label</span><span class="tag">></span></font></strong><span>n"; </span> </li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> name="</span><span class="attribute-value"><font color="#0000ff">".$name."</font></span><span>" </span><span class="attribute"><font color="#ff0000">value</font></span><span>="".$value."" </span><span class="attribute"><font color="#ff0000">type</font></span><span>=""<br>.$type."" </span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>="".$maxlength."" </span><span class="attribute"><font color="#ff0000">required</font></span><span>="".$required."" </span><span class="attribute"><font color="#ff0000">check</font></span><span>=""<br>.$check."" </span><span class="attribute"><font color="#ff0000">id</font></span><span>="".$id."" </span><span class="attribute"><font color="#ff0000">class</font></span><span>="input" ".$readonly." </span><span class="attribute"><font color="#ff0000">style</font></span><span>="width:".$width.<br>"px;" </span><span class="attribute"><font color="#ff0000">showName</font></span><span>="".$title."" </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span> "; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">span</span></span></font></strong><span> class="</span><span class="attribute-value"><font color="#0000ff">tip</font></span><span>"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>".$tip."</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span></font></strong><span>n"; </span> </li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"</font></span> </li>n"<span>; </span><li class=""><span>} </span></li> </ol>
这是我写的表单类中一个插入文本框的函数.
PHP函数参数传递方法的调用方法为
<ol class="dp-xml"><li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput("编码","field0","","text",3,""); </span></span></li></ol>
在开始的时候只预留了$title,$name,$value,$type,$maxlength,$readonly等参数,经过一段时间的使用,发现这些基本参数无法满足需求,文本框需要有js验证,需要定义CSS样式,需要增加提示信息等...
增加了$required,$check,$id,$width,$tip等参数之后发现以前所有调用此函数的地方都需要修改,增加了很多工作量.
PHP函数参数传递方法的调用方法变成
<ol class="dp-xml"><li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput("编码","field0","","text",3,"","true",""<br>,"",100,"提示:编号为必填项,只能填写3位"); </span></span></li></ol>
如果使用这个函数的地方很多的话一个一个改确实需要很长时间.
下面是我改进之后的函数
<ol class="dp-xml"> <li class="alt"><span><span>function addInput($a) </span></span></li> <li class=""><span>{ </span></li> <li class="alt"><span> if(is_array($a)) </span></li> <li class=""><span> { </span></li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">title</font></span><span> = $a['title']; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">name</font></span><span> = $a['name']; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">value</font></span><span> = $a['value'] ? $a['value'] : ""; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">type</font></span><span> = $a['type'] ? $a['type'] : "text"; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span> = $a['maxlength'] ? $a['maxlength'] : "255"; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">readonly</font></span><span> = $a['readonly'] ? $a['readonly'] : ""; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">required</font></span><span> = $a['required'] ? $a['required'] : "false"; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">check</font></span><span> = $a['check']; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">id</font></span><span> = $a['id']; </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">width</font></span><span> = $a['width']; </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">tip</font></span><span> = $a['tip']; </span> </li> <li class=""><span> } </span></li> <li class="alt"> <span> $title,$name,$</span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">""</font></span><span>,$</span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"text"</font></span><span>,$</span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"255"</font></span><span>,$readonly,$</span><span class="attribute"><font color="#ff0000">required</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"false"</font></span><span>,$check,$id,$width,$tip </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<li>n"</li></font></span><span>; </span> </li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"<label>"</label></font></span><span>.$title.":</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">label</span><span class="tag">></span></font></strong><span>n"; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></span></font></strong><span> name="</span><span class="attribute-value"><font color="#0000ff">".$name."</font></span><span>" </span><span class="attribute"><font color="#ff0000">value</font></span><span>="".$value."" </span><span class="attribute"><font color="#ff0000">type</font></span><span>="".$type."" </span><span class="attribute"><font color="#ff0000">maxlength</font></span><span>="".$maxlength."" </span><span class="attribute"><font color="#ff0000">required</font></span><span>="".$required."" </span><span class="attribute"><font color="#ff0000">check</font></span><span>="".$check."" </span><span class="attribute"><font color="#ff0000">id</font></span><span>="".$id."" </span><span class="attribute"><font color="#ff0000">class</font></span><span>="input" ".$readonly." </span><span class="attribute"><font color="#ff0000">style</font></span><span>="width:".$width."px;" </span><span class="attribute"><font color="#ff0000">showName</font></span><span>="".$title."" </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span> "; </span> </li> <li class="alt"> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= "</span><strong><font color="#006699"><span class="tag"><span class="tag-name">span</span></span></font></strong><span> class="</span><span class="attribute-value"><font color="#0000ff">tip</font></span><span>"</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>".$tip."</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">span</span><span class="tag">></span></font></strong><span>n"; </span> </li> <li class=""> <span> $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>form </span><span class="attribute"><font color="#ff0000">.</font></span><span>= </span><span class="attribute-value"><font color="#0000ff">"</font></span> </li>n"<span>; </span><li class="alt"><span>} </span></li> </ol>
调用方法变为
<ol class="dp-xml"> <li class="alt"><span><span>$form-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>addInput( </span></span></li> <li class=""><span> array( </span></li> <li class="alt"><span> 'title' = "编码", </span></li> <li class=""><span> 'name' = "field0", </span></li> <li class="alt"><span> 'maxlength' = 3, </span></li> <li class=""><span> 'required' = "true", </span></li> <li class="alt"><span> 'width' = 100, </span></li> <li class=""><span> 'tip' = "提示:编号为必填项,只能填写3位", </span></li> <li class="alt"><span> ) </span></li> <li class=""><span>); </span></li> </ol>
经过前后PHP函数参数传递方法的对比可以发现:
传统的函数在需要扩展的时候改动量大,使用的时候必须按参数的顺序写,很容易出错.
改进后的函数扩展的时候可以随时增加新参数,只需要在调用时增加对应的数组键值,每个参数都一目了然,无需考虑顺序,代码可读性增强.
不过PHP函数参数传递方法的改进还是有缺点的,代码量增大了,需要程序员多写很多键值,还有就是函数中判断语句和三元运算语句可能会影响效率.