Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

either/or function param 1

Status
Not open for further replies.

ThomasJSmart

Programmer
Sep 16, 2002
634
i have a function that can take a path or string data as an attribute. Just wondering if there is a "right" way to do this (and why).


Similar functions in php seem to take the 2 function route
Code:
set_data_with_path($path)
set_data_with_string($string)

but this seems a little cumbersome for a developer implementing it, they need to make sure they use the right function.
and it seems to open the door to duplicate code or needing a bunch of followup functions that both initial functions use with further complicates the code.

A single function solution could be to have 2 optional params, and a check to make sure one of them is filled in
Code:
function set_data($path=false,$string=false){
 if(!$path && !$string){ 
  // fail
 }
}
but for some reason this feels a bit messy


from a "using the function" perspective it would be ideal to just have 1 param
Code:
function set_data($path_or_string){
 // and then some magic to check if its a path or a string
}
but this seems a bit unstable and error prone, even more so if the params would be 2 different types of string instead of 1 path - that could be easier recognized.


another option, but probably less intuitive, would be an array
Code:
function set_data($path_or_string){   //array('path' => $path)  or array('string' => $string)
 // we can tell by the key which one it is
}

a last one that i can think of is the data and an identifier param
Code:
function set_data($path_or_string, $type){
 // we can tell by the $type which one it is
}

Any other ideas or thoughts on this?

Thanks



site | / blog |
 
the rationale for having two functions may be because they are abstracted from a central code base. php strives to make its function naming user accessible and obvious. you will probably find that both functions point to the same code in the core so that, in fact, there is no code duplication.

e.g.
Code:
function doSomething_URL($url){
  if(file_exists($url) && is_readable($url)):
    return doSomething_String(file_get_contents($url));
  endifl
}

function doSomething_String($string){
  //do lots of things
}

if you are talking about building code for your own projects, then the key determining factor should be how you think about the codebase and what the easiest way is for you to manage it. what will be more obvious to you as a convention when you come back to the code in a year's time? etc

more on point observations include:

1. i think you will find it difficult to distinguish programmatically between a url and a string. after all, a string can look like a url, be a valid url, and even (if treated as a url) point to a valid resource, but still be intended by the user to be a string. So I would counsel against a programmatic approach unless you can be certain that these use cases cannot occur.

all the others look fine to me. But I'd be tempted to go with the 'ordinary' form of two functions, one pointing to the other.
 
after trying out a few of them i was reaching the same conclusion, but it still feels like the lesser of all evils :/

There are specific instances where the either/or works though, certainly if its a different type (array/string for example) but like you say, for 2 strings it becomes risky.

2 functions it is!

thanks

site | / blog |
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top