偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

PHP函數(shù)參數(shù)傳遞方法的具體改進技巧分享

開發(fā) 后端
我們接下來要為大家介紹的是使用數(shù)組對PHP函數(shù)參數(shù)傳遞方法進行改進。改進后的函數(shù)能夠讓用戶隨意增加新的參數(shù),并且無需考慮順序。

當我們在寫PHP代碼的時候,經常會需要對代碼進行多次的升級更改等,這樣來回不斷的重復修改參數(shù),會使我們的整個程序性能降低,并增加了不少的工作量。我們今天就為大家介紹一下是使用數(shù)組進行PHP函數(shù)參數(shù)傳遞方法的趕緊。

#t#本人在經歷了多次重復操作之后決定改進一下傳統(tǒng)PHP函數(shù)參數(shù)傳遞方法,使用數(shù)組作為參數(shù),請看下面的例子.

先看一個傳統(tǒng)的自定義函數(shù)

  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 ."<li>\n";  
  20.     $this->form ."<label>".$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 ."</li>\n";  

這是我寫的表單類中一個插入文本框的函數(shù).

PHP函數(shù)參數(shù)傳遞方法的調用方法為

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

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

增加了$required,$check,$id,$width,$tip等參數(shù)之后發(fā)現(xiàn)以前所有調用此函數(shù)的地方都需要修改,增加了很多工作量.

PHP函數(shù)參數(shù)傳遞方法的調用方法變成

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

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

下面是我改進之后的函數(shù)

  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 ."<li>\n";  
  19.     $this->form ."<label>".$title.":</label>\n";  
  20.     $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."\" /> ";  
  21.     $this->form .= "<span class=\"tip\">".$tip."</span>\n";  
  22.     $this->form ."</li>\n";  

調用方法變?yōu)?/STRONG>

  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函數(shù)參數(shù)傳遞方法的對比可以發(fā)現(xiàn):

傳統(tǒng)的函數(shù)在需要擴展的時候改動量大,使用的時候必須按參數(shù)的順序寫,很容易出錯.

改進后的函數(shù)擴展的時候可以隨時增加新參數(shù),只需要在調用時增加對應的數(shù)組鍵值,每個參數(shù)都一目了然,無需考慮順序,代碼可讀性增強.

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

責任編輯:曹凱 來源: 藍色理想
相關推薦

2009-12-01 10:50:45

PHP函數(shù)requir

2009-12-01 14:26:19

PHP函數(shù)ob_sta

2009-11-25 17:28:26

PHP對話

2009-12-08 14:00:11

PHP函數(shù)microt

2009-12-07 19:34:01

PHP函數(shù)可變參數(shù)列表

2009-12-03 17:18:15

PHP strtoti

2009-11-27 09:30:58

PHP函數(shù)mb_str

2009-12-07 16:52:59

PHP函數(shù)getima

2009-12-07 14:29:08

PHP array_w

2010-03-11 11:07:37

Python函數(shù)參數(shù)

2009-12-02 10:08:33

PHP mail()函

2009-11-26 19:05:04

PHP函數(shù)explod

2009-11-26 15:23:24

PHP函數(shù)ereg()

2009-12-09 17:33:22

PHP性能優(yōu)化

2009-11-23 18:47:51

PHP中用header

2009-12-02 15:50:41

PHP抓取網(wǎng)頁內容

2009-12-01 19:23:22

PHP緩存技術

2011-07-11 10:24:09

PHP

2009-12-01 19:02:20

PHP取整函數(shù)

2025-04-02 12:00:00

開發(fā)日志記錄Python
點贊
收藏

51CTO技術棧公眾號