自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

當(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ù)

  1. /**  
  2. * @Purpose:     插入文本域  
  3. * @Method Name: addInput()  
  4. * @Parameter:    str $title        表單項(xiàng)標(biāo)題  
  5. * @Parameter:    str $name        元素名稱  
  6. * @Parameter:    str $value        默認(rèn)值  
  7. * @Parameter:    str $type        類型,默認(rèn)為text,可選password  
  8. * @Parameter:    str $maxlength        最長輸入  
  9. * @Parameter:    str $readonly        只讀  
  10. * @Parameter:    str $required        是否必填,默認(rèn)為false,true為必填  
  11. * @Parameter:    str $check        表單驗(yàn)證function(js)名稱  
  12. * @Parameter:    str $id            元素id,無特殊需要時(shí)省略  
  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";  

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

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

  1. $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)用方法變成

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

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

下面是我改進(jìn)之后的函數(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";  

調(diào)用方法變?yōu)?/STRONG>

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

經(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ì)影響效率.

責(zé)任編輯:曹凱 來源: 藍(lán)色理想
相關(guān)推薦

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對(duì)話

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 14:29:08

PHP array_w

2009-12-07 16:52:59

PHP函數(shù)getima

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-01 19:02:20

PHP取整函數(shù)

2009-12-01 19:23:22

PHP緩存技術(shù)

2011-07-11 10:24:09

PHP

2025-04-02 12:00:00

開發(fā)日志記錄Python

2009-12-02 15:50:41

PHP抓取網(wǎng)頁內(nèi)容
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)