I had to do something similar with LDAP. What happened was I needed an abstract function to see if someone was a member of a group, and some scripts that called this function would already have their own LDAP connect open, while simpler, more generic scripts would most certainly not have their own link open.
So, I used a function with a variable number of arguments. If the function was called with two args, then it created its own connection to the LDAP server and ran the search, if it was called with three, the third argument was assumed to be connection handle and that connection was used.
function group_member(){
# $grp, $uid, [$link] <- arg list
$argc = func_num_args();
if ($argc<2||$argc>3) die("2 or 3 parameters only");
$grp = func_get_arg(0);
$uid = func_get_arg(1);
if ($argc==3) $link = func_get_arg(2);
else {
include_once "ldap/bind.inc";
$link = llink();
}
To answer your original question, the third method is both the cleanest and the fastest. It's bad programming form to open connections you don't need etc... Just use something like the above code in a function to remove that necessity.