生活随笔
收集整理的這篇文章主要介紹了
PHP函数参数传递方法的具体改进技巧
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?本人在經(jīng)歷了多次重復操作之后決定改進一下傳統(tǒng)PHP函數(shù)參數(shù)傳遞方法,使用數(shù)組作為參數(shù),請看下面的例子.
先看一個傳統(tǒng)的自定義函數(shù)
<?php
/** ?*?@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";
}? ?
這是我寫的表單類中一個插入文本框的函數(shù).
PHP函數(shù)參數(shù)傳遞方法的調(diào)用方法為
$form->addInput("編碼","field0","","text",3,"");?
在開始的時候只預留了$title,$name,$value,$type,$maxlength,$readonly等參數(shù),經(jīng)過一段時間的使用,發(fā)現(xiàn)這些基本參數(shù)無法滿足需求,文本框需要有js驗證,需要定義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,"提示:編號為必填項,只能填寫3位"); ?
如果使用這個函數(shù)的地方很多的話一個一個改確實需要很長時間.
下面是我改進之后的函數(shù)
<?php
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)?/p>
$form->addInput( ?array( ?'title'?=?"編碼", ?'name'?=?"field0", ?'maxlength'?=?3, ?'required'?=?"true", ?'width'?=?100, ?'tip'?=?"提示:編號為必填項,只能填寫3位", ?) ?
); ? ?
經(jīng)過前后PHP函數(shù)參數(shù)傳遞方法的對比可以發(fā)現(xiàn):
傳統(tǒng)的函數(shù)在需要擴展的時候改動量大,使用的時候必須按參數(shù)的順序?qū)?很容易出錯.
改進后的函數(shù)擴展的時候可以隨時增加新參數(shù),只需要在調(diào)用時增加對應的數(shù)組鍵值,每個參數(shù)都一目了然,無需考慮順序,代碼可讀性增強.
不過PHP函數(shù)參數(shù)傳遞方法的改進還是有缺點的,代碼量增大了,需要程序員多寫很多鍵值,還有就是函數(shù)中判斷語句和三元運算語句可能會影響效率.
?
總結
以上是生活随笔為你收集整理的PHP函数参数传递方法的具体改进技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。