前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP函数参数传递方法的具体改进技巧

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

作者头像
用户7657330
发布2020-08-14 15:57:17
2K0
发布2020-08-14 15:57:17
举报
文章被收录于专栏:程序生涯程序生涯程序生涯

本人在经历了多次重复操作之后决定改进一下传统PHP函数参数传递方法,使用数组作为参数,请看下面的例子.

先看一个传统的自定义函数

<?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. 这是我写的表单类中一个插入文本框的函数.

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

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

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

增加了required,check,id,width,

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

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

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

下面是我改进之后的函数

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

调用方法变为

$form->addInput(  
    array(  
        'title' = "编码",  
        'name' = "field0",  
        'maxlength' = 3,  
        'required' = "true",  
        'width' = 100,  
        'tip' = "提示:编号为必填项,只能填写3位",  
    )  
);  

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

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

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

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档