PHP函数参数传递方法的具体改进技巧分享

开发 后端
我们接下来要为大家介绍的是使用数组对PHP函数参数传递方法进行改进。改进后的函数能够让用户随意增加新的参数,并且无需考虑顺序。

当我们在写PHP代码的时候,经常会需要对代码进行多次的升级更改等,这样来回不断的重复修改参数,会使我们的整个程序性能降低,并增加了不少的工作量。我们今天就为大家介绍一下是使用数组进行PHP函数参数传递方法的赶紧。

#t#本人在经历了多次重复操作之后决定改进一下传统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";  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.

这是我写的表单类中一个插入文本框的函数.

PHP函数参数传递方法的调用方法为

$form->addInput("编码","field0","","text",3,""); 
  • 1.

在开始的时候只预留了$title,$name,$value,$type,$maxlength,$readonly等参数,经过一段时间的使用,发现这些基本参数无法满足需求,文本框需要有js验证,需要定义CSS样式,需要增加提示信息等...

增加了$required,$check,$id,$width,$tip等参数之后发现以前所有调用此函数的地方都需要修改,增加了很多工作量.

PHP函数参数传递方法的调用方法变成

$form->addInput("编码","field0","","text",3,"","true",""
,"",100,"提示:编号为必填项,只能填写3位");  
  • 1.
  • 2.

如果使用这个函数的地方很多的话一个一个改确实需要很长时间.

下面是我改进之后的函数

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";  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

调用方法变为

$form->addInput(  
    array(  
        'title' = "编码",  
        'name' = "field0",  
        'maxlength' = 3,  
        'required' = "true",  
        'width' = 100,  
        'tip' = "提示:编号为必填项,只能填写3位",  
    )  
);  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

经过前后PHP函数参数传递方法的对比可以发现:

传统的函数在需要扩展的时候改动量大,使用的时候必须按参数的顺序写,很容易出错.

改进后的函数扩展的时候可以随时增加新参数,只需要在调用时增加对应的数组键值,每个参数都一目了然,无需考虑顺序,代码可读性增强.

不过PHP函数参数传递方法的改进还是有缺点的,代码量增大了,需要程序员多写很多键值,还有就是函数中判断语句和三元运算语句可能会影响效率.

责任编辑:曹凯 来源: 蓝色理想
相关推荐

2009-12-01 10:50:45

PHP函数requir

2009-12-01 14:26:19

PHP函数ob_sta

2009-11-25 17:28:26

PHP对话

2009-12-08 14:00:11

PHP函数microt

2009-12-07 19:34:01

PHP函数可变参数列表

2009-12-03 17:18:15

PHP strtoti

2009-11-27 09:30:58

PHP函数mb_str

2009-12-07 16:52:59

PHP函数getima

2009-12-07 14:29:08

PHP array_w

2010-03-11 11:07:37

Python函数参数

2009-11-26 15:23:24

PHP函数ereg()

2009-11-26 19:05:04

PHP函数explod

2009-12-02 10:08:33

PHP mail()函

2009-12-01 19:02:20

PHP取整函数

2009-11-23 18:47:51

PHP中用header

2009-12-09 17:33:22

PHP性能优化

2009-12-02 15:50:41

PHP抓取网页内容

2011-07-11 10:24:09

PHP

2009-12-01 19:23:22

PHP缓存技术

2009-11-24 20:09:35

PHP函数header
点赞
收藏

51CTO技术栈公众号