tags:

views:

34

answers:

1

I'm reading Master Joomla! 1.5 book, and I notice in the Revue model has a function like that

// model
function getRevues() {
  $db =& $this->_db;
  if( empty($this->_revues) ) // ?????
  {
    $query = $this->_buildQuery();
    $limitstart = $this->getState('limitstart');
    $limit = $this->getState('limit');

    $this->_revues = $this->_getLimit($query, $limitstart, $limit);
  }
  return $this->_revues;
}

// view
....
revues =& model->getRevues();

why use _revues variable in class model? If I remove _revues variable and rewrite getRevues function as follows:

function getRevues() {
  $db =& $this->_db;

    $query = $this->_buildQuery();
    $limitstart = $this->getState('limitstart');
    $limit = $this->getState('limit');

    $revues = $this->_getLimit($query, $limitstart, $limit);

  return &$revues;
}

what difference between 2 functions?

+2  A: 

In the second one, you always execute the database query. In the first one you cache the results (in $this->_revues)which might increase performance of the application. So the database is only hit if you call this method the first time.

Felix Kling