|
Posted by J.O. Aho on 11/11/06 12:55
sTony wrote:
> I've now got several versions of what is virtually the same function. I've
> got a createLink($Link); and a createSpecialLink($Link,$Name) and a
> createExtraLink($Link,$Extra) and now I've come across a situation where I
> need to write yet another almost exactly the same function. I know that php
> must support functions with a variable number of parameters, but I have no
> idea how to write one. How is this done in php, so I can have just one
> function that does different things according to the information supplied.
/* put the functions which variable you use most often first */
function createLink($Link,$Name="",$Extra="",$NewExtra) {
if(!empty($Name)) {
/* Do this */
} else if((!empty($Extra)) {
/* Do that */
} else if((!empty($NewExtra)) {
/* did this */
} else {
/* do this if nothing */
}
}
createLink($Link);
createLink($Link,$Name);
createLink($Link,"",$Extra);
createLink($Link,"","",$NewExtra);
You may not want that, but have as few arguments as possible, then
function createLink($Link,$dowhat=0,$AddThis="") {
switch($dowhat) {
case 1:
$Name=$AddThis;
/* Do this */
break;
case 2:
$Extra=$AddThis;
/* Do that */
break;
case 3:
$NewExtra=$AddThis;
/* did this */
break;
default:
/* do this if nothing */
break;
}
}
createLink($Link);
createLink($Link,1,$Name);
createLink($Link,2,$Extra);
createLink($Link,3,$NewExtra);
Just experiment and do it the way you want it.
//Aho
[Back to original message]
|