为什么在模型中使用变量(MVC)?
问题描述:
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?
我正在读Joomla大师! 1.5本书,我注意到Revue模型中有这样的函数 p>
// 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);
}
返回$ this-> _revues;
}
// view
....
revues =& model-> getRevues();
code> pre>
为什么在类模型中使用_revues变量? 如果我删除_revues变量并重写getRevues函数,如下所示: p>
function getRevues(){
$ db =& $ this-> _db;
$ query = $ this-> _buildQuery();
$ limitstart = $ this-> getState('limitstart');
$ limit = $ this-> getState('limit');
$ revues = $ this-> _getLimit($ query,$ limitstart,$ limit);
return& $ revues;
}
code> pre>
2个函数有什么区别? p>
div>
答
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.