In investigating an unrelated bug #441744: Better language support for multi-lingual (i18n) sites, I ran across a piece of code that puzzled me...

jobtrack.module @ line 282:

/**
 * Menu callback, load a ticket.
 */
function jobtrack_ticket_load($nid) {
  $tickets = array();
  if (!isset($tickets[$nid])) {
    $tickets[$nid] = db_fetch_object(db_query('SELECT * FROM {jobtrack_ticket} WHERE nid = %d', $nid));
  }
  return $tickets[$nid];
}

It looks like that's a typical coding pattern for a static variable where you want to cache all the $ticket ids ... but its missing the staticidentifier in front of $tickets = array();

There is another function (just above this one) that is nearly the same code pattern, but does have the static identifier....

/**
 * Menu callback, load a client.
 */
function jobtrack_client_load($clid) {
  static $clients = array();
  if (!isset($clients[$clid])) {
    $clients[$clid] = db_fetch_object(db_query('SELECT clid, name, path, status, integrate_email, server_name, server_username, server_password, mailfrom, mailbox, protocol, port, notes, autosubscribe, domains FROM {jobtrack_client} WHERE clid = %d', $clid));
  }
  return $clients[$clid];
}

Just curious about this bit of code if its a typo, or just incomplete. Furthermore, I'm no "pro" at PHP but I would think that doing something like static $clients = array(); would reinitialize the array each time, effectively defeating the purpose of the static identifier. ... again i could be wrong, AND IF SO, PLEASE POINT ME IN THE RIGHT DIRECTION :) ... but I would do something like this:

function jobtrack_client_load($clid) {
  static $clients;
  $clients = is_array($clients) ? $clients : array();
  //... proceed with rest of code ...
}

Am I completely off here? Again, im not trying to be a code snob, but just trying to learn and understand Drupal the best i can :)

Comments

jeremy’s picture

Status: Active » Fixed

You are correct about the first part -- I intended to statically cache loaded tickets. Fix committed. Thanks!

As for statics, the assignment only happens the first time the function is called. Find more information about how static's work here:
http://php.net/static

jwilson3’s picture

cool, thanks for pointing that out. learn something new every day :)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.