PHP函數(shù)參數(shù)傳遞方法的具體改進(jìn)技巧分享
當(dāng)我們?cè)趯?A >PHP代碼的時(shí)候,經(jīng)常會(huì)需要對(duì)代碼進(jìn)行多次的升級(jí)更改等,這樣來回不斷的重復(fù)修改參數(shù),會(huì)使我們的整個(gè)程序性能降低,并增加了不少的工作量。我們今天就為大家介紹一下是使用數(shù)組進(jìn)行PHP函數(shù)參數(shù)傳遞方法的趕緊。
#t#本人在經(jīng)歷了多次重復(fù)操作之后決定改進(jìn)一下傳統(tǒng)PHP函數(shù)參數(shù)傳遞方法,使用數(shù)組作為參數(shù),請(qǐng)看下面的例子.
先看一個(gè)傳統(tǒng)的自定義函數(shù)
- /**
- * @Purpose: 插入文本域
- * @Method Name: addInput()
- * @Parameter: str $title 表單項(xiàng)標(biāo)題
- * @Parameter: str $name 元素名稱
- * @Parameter: str $value 默認(rèn)值
- * @Parameter: str $type 類型,默認(rèn)為text,可選password
- * @Parameter: str $maxlength 最長輸入
- * @Parameter: str $readonly 只讀
- * @Parameter: str $required 是否必填,默認(rèn)為false,true為必填
- * @Parameter: str $check 表單驗(yàn)證function(js)名稱
- * @Parameter: str $id 元素id,無特殊需要時(shí)省略
- * @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";
- }
這是我寫的表單類中一個(gè)插入文本框的函數(shù).
PHP函數(shù)參數(shù)傳遞方法的調(diào)用方法為
- $form->addInput("編碼","field0","","text",3,"");
在開始的時(shí)候只預(yù)留了$title,$name,$value,$type,$maxlength,$readonly等參數(shù),經(jīng)過一段時(shí)間的使用,發(fā)現(xiàn)這些基本參數(shù)無法滿足需求,文本框需要有js驗(yàn)證,需要定義CSS樣式,需要增加提示信息等...
增加了$required,$check,$id,$width,$tip等參數(shù)之后發(fā)現(xiàn)以前所有調(diào)用此函數(shù)的地方都需要修改,增加了很多工作量.
PHP函數(shù)參數(shù)傳遞方法的調(diào)用方法變成
- $form->addInput("編碼","field0","","text",3,"","true",""
,"",100,"提示:編號(hào)為必填項(xiàng),只能填寫3位");
如果使用這個(gè)函數(shù)的地方很多的話一個(gè)一個(gè)改確實(shí)需要很長時(shí)間.
下面是我改進(jìn)之后的函數(shù)
- 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";
- }
調(diào)用方法變?yōu)?/STRONG>
經(jīng)過前后PHP函數(shù)參數(shù)傳遞方法的對(duì)比可以發(fā)現(xiàn):
傳統(tǒng)的函數(shù)在需要擴(kuò)展的時(shí)候改動(dòng)量大,使用的時(shí)候必須按參數(shù)的順序?qū)?很容易出錯(cuò).
改進(jìn)后的函數(shù)擴(kuò)展的時(shí)候可以隨時(shí)增加新參數(shù),只需要在調(diào)用時(shí)增加對(duì)應(yīng)的數(shù)組鍵值,每個(gè)參數(shù)都一目了然,無需考慮順序,代碼可讀性增強(qiáng). 不過PHP函數(shù)參數(shù)傳遞方法的改進(jìn)還是有缺點(diǎn)的,代碼量增大了,需要程序員多寫很多鍵值,還有就是函數(shù)中判斷語句和三元運(yùn)算語句可能會(huì)影響效率.