PHP函數參數傳遞方法的具體改進技巧分享_PHP教程

來源:互聯網
上載者:User
當我們在寫本人在經曆了多次重複操作之後決定改進一下傳統PHP函數參數傳遞方法,使用數組作為參數,請看下面的例子.

先看一個傳統的自訂函數

 
  1. /**
  2. * @Purpose: 插入文本域
  3. * @Method Name: addInput()
  4. * @Parameter: str $title 表單項標題
  5. * @Parameter: str $name 元素名稱
  6. * @Parameter: str $value 預設值
  7. * @Parameter: str $type 類型,預設為text,可選password
  8. * @Parameter: str $maxlength 最長輸入
  9. * @Parameter: str $readonly 唯讀
  10. * @Parameter: str $required 是否必填,預設為false,true為必填
  11. * @Parameter: str $check 表單驗證function(js)名稱
  12. * @Parameter: str $id 元素id,無特殊需要時省略
  13. * @Parameter: int $width 元素寬度,單位:象素
  14. * @Parameter: str $tip 元素提示資訊
  15. * @Return:
  16. */
  17. function addInput($title,$name,$value="",$type="text",$maxlength="255",
    $readonly,$required="false",$check,$id,$width,$tip)
  18. {
  19. $this->form .= "
  20. n";
  21. $this->form .= "".$title.":label>n";
  22. $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."" /> ";
  23. $this->form .= "<span class="tip">".$tip."span>n";
  24. $this->form .= "
  25. n";
  26. }

這是我寫的表單類中一個插入文字框的函數.

PHP函數參數傳遞方法的調用方法為

 
  1. $form->addInput("編碼","field0","","text",3,"");

在開始的時候只預留了$title,$name,$value,$type,$maxlength,$readonly等參數,經過一段時間的使用,發現這些基本參數無法滿足需求,文字框需要有js驗證,需要定義CSS樣式,需要增加提示資訊等...

增加了$required,$check,$id,$width,$tip等參數之後發現以前所有調用此函數的地方都需要修改,增加了很多工作量.

PHP函數參數傳遞方法的調用方法變成

 
  1. $form->addInput("編碼","field0","","text",3,"","true",""
    ,"",100,"提示:編號為必填項,只能填寫3位");

如果使用這個函數的地方很多的話一個一個改確實需要很長時間.

下面是我改進之後的函數

 
  1. function addInput($a)
  2. {
  3. if(is_array($a))
  4. {
  5. $title = $a['title'];
  6. $name = $a['name'];
  7. $value = $a['value'] ? $a['value'] : "";
  8. $type = $a['type'] ? $a['type'] : "text";
  9. $maxlength = $a['maxlength'] ? $a['maxlength'] : "255";
  10. $readonly = $a['readonly'] ? $a['readonly'] : "";
  11. $required = $a['required'] ? $a['required'] : "false";
  12. $check = $a['check'];
  13. $id = $a['id'];
  14. $width = $a['width'];
  15. $tip = $a['tip'];
  16. }
  17. $title,$name,$value="",$type="text",$maxlength="255",$readonly,$required="false",$check,$id,$width,$tip
  18. $this->form .= "
  19. n";
  20. $this->form .= "".$title.":label>n";
  21. $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."" /> ";
  22. $this->form .= "<span class="tip">".$tip."span>n";
  23. $this->form .= "
  24. n";
  25. }

調用方法變為

 
  1. $form->addInput(
  2. array(
  3. 'title' = "編碼",
  4. 'name' = "field0",
  5. 'maxlength' = 3,
  6. 'required' = "true",
  7. 'width' = 100,
  8. 'tip' = "提示:編號為必填項,只能填寫3位",
  9. )
  10. );

經過前後PHP函數參數傳遞方法的對比可以發現:

傳統的函數在需要擴充的時候改動量大,使用的時候必須按參數的順序寫,很容易出錯.

改進後的函數擴充的時候可以隨時增加新參數,只需要在調用時增加對應的數組索引值,每個參數都一目瞭然,無需考慮順序,代碼可讀性增強.

不過PHP函數參數傳遞方法的改進還是有缺點的,代碼量增大了,需要程式員多寫很多索引值,還有就是函數中判斷語句和三元運算語句可能會影響效率.


http://www.bkjia.com/PHPjc/446329.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446329.htmlTechArticle當我們在寫 本人在經曆了多次重複操作之後決定改進一下傳統PHP函數參數傳遞方法,使用數組作為參數,請看下面的例子. 先看一個傳統的自...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.