Question

What do you do (in recent PHP) when you want to call a function with byref arguments only some of the time?

function blah_byval ($arg) { /* some code */ }
function blah_byref (&$arg) { /* same code */ }

Seems retarded to me. I must be missing something.

I do have a use-case, although the question is basically hypothetical. Here's a captcha implementation-in-progress:

function captcha_image($string = "", $create = true, $session = "") {

  # outputs a transparent gif image 16 pixels high by 60 pixels wide containing
  # a string of six lowercase letters, either specified by $string or generated
  # randomly. returns this string, or false in the event of an error.

  # once a captcha image is created, the string it contains should be saved so
  # that it can be compared later with user input. to skip saving, set $create
  # to false.

  # if called from a web server, content-type is set.

  if (!preg_match("/^[a-z]{6}$/", $string)) $string = string_random(6);
  if (php_sapi_name() != "cli") header("Content-Type: image/gif");
  if ($ihandle = @imagecreatetruecolor(60, 16)) {
    $background = imagecolorallocate($ihandle, 255, 255, 255);
    $foreground = imagecolorallocate($ihandle, 128, 128, 128);
    imagecolortransparent($ihandle, $background);
    imagefilledrectangle($ihandle, 0, 0, 60, 16, $background);
    imagestring($ihandle, 5, 0, 0, $string, $foreground);
    imagegif($ihandle, NULL);
    imagedestroy($ihandle);
    if ($create) return captcha_create($string, $session);
    return $string;
  }
  return false;
}

function captcha_compare($string, $session = "", $destroy = true) {

  # returns true if $string is associated with a certain session, which is
  # specified by $session or, if no such session exists, by session_identity().
  # if $string is not associated with the session, returns false.

  # once a string is matched to a session, it should be destroyed to prevent
  # re-use. to allow re-use, $destroy may be set to false.

  global $cfgimage;
  if (!session_exists($session)) $session = session_identity();
  $get_return = false;
  if ($chandle = @fopen($cfgimage['captcha']['file'], "rb")) {
    flock($chandle, LOCK_SH);
    while (!feof($chandle)) {
      $line = explode(" ", trim(fgets($chandle)), 3);
      if (($line[2] === $string) && ($line[1] == $session) && ($line[0] > (time() - $cfgimage['captcha']['keepalive']))) {
        $get_return = true;
        break;
      }
    }
    fclose($chandle);
  }
  if (($destroy) && ($get_return)) captcha_destroy($session);
  return $get_return;
}

function captcha_create($string, $session = "") {

  # associates a string of six lowercase letters with a session, either
  # specified by $session or by session_identity(). fails if $string is not
  # well-formed. returns true if successful, or false.

  global $cfgimage;
  if (!preg_match("/^[a-z]{6}$/", $string)) return false;
  if (!session_exists($session)) $session = session_identity();
  if ($thandle = @tmpfile()) {
    if ($chandle = @fopen($cfgimage['captcha']['file'], "rb")) {
      flock($chandle, LOCK_SH);
      while (!feof($chandle)) {
        $line = explode(" ", trim(fgets($chandle)), 3);
        if (($line[1] != $session) && ($line[0] > (time() - $cfgimage['captcha']['keepalive']))) fputs($thandle, implode(" ", $line) . "\n");
      }
      fclose($chandle);
      fseek($thandle, 0);
      if ($chandle = @fopen($cfgimage['captcha']['file'], "cb")) {
        if (flock($chandle, LOCK_EX)) {
          fputs($chandle, time() . " " . $session . " " . $string . "\n");
          while (!feof($thandle)) fputs($chandle, fgets($thandle));
        }
        fclose($chandle);
      }
    }
    flock($thandle, LOCK_UN);
    fclose($thandle);
    return true;
  }
  return false;
}

function captcha_destroy($session = "") {

  # dis-associates any existing captcha string with a session, either specified
  # by $session or by session_identity(). does not return any value.

  global $cfgimage;
  if (!session_exists($session)) $session = session_identity();
  if ($thandle = @tmpfile()) {
    if ($chandle = @fopen($cfgimage['captcha']['file'], "rb")) {
      flock($chandle, LOCK_SH);
      while (!feof($chandle)) {
        $line = explode(" ", trim(fgets($chandle)), 3);
        if (($line[1] != $session) && ($line[0] > (time() - $cfgimage['captcha']['keepalive']))) fputs($thandle, implode(" ", $line) . "\n");
      }
      fclose($chandle);
      fseek($thandle, 0);
      if ($chandle = @fopen($cfgimage['captcha']['file'], "cb")) {
        if (flock($chandle, LOCK_EX)) {
          while (!feof($thandle)) fputs($chandle, fgets($thandle));
        }
        fclose($chandle);
      }
    }
    flock($thandle, LOCK_UN);
    fclose($thandle);
  }
}

In captcha_image(), return captcha_create($string, $session) used to be captcha_create(&$string ...).

Was it helpful?

Solution

This is not possible in userland code. For internal functions on the hand you can specify the PREFER_REF flag in the arginfo struct. This will pass the value by-ref if possible and by-val otherwise.

In my eyes it is good that this feature is not exposed to userland code. References are a tricky business in any case and making them optional with different behavior depending on how exactly you pass stuff in only makes it worse.

My suggestion: Don't use references altogether. You'll only have problems with them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top