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
.
=
"<li>\n"
;
19.
$this-
>
form
.
=
"<label>"
.$title.":
</label>
\n";
20.
$this-
>
form
.
= "
<input
name=\"
".$name."
\"
value
=\"".$value."\"
typ
e
=\"".$type."\"
maxlength
=\"".$maxlength."\"
required
=\"".$required."\"
check
=\"".$check."\"
id
=\"".$id."\"
class
=\"input\" ".$readonly."
styl
e
=\"width:".$width."px;\"
showName
=\"".$title."\"
/>
";
21.
$this-
>
form
.
= "
<span
class=\"
tip
\"
>
".$tip."
</span>
\n";
22.
$this-
>
form
.
=
"</li>\n"
;
23. }