• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Does assigning Model variables reduce load?

Jaxel

Well-known member
#1
I would normally do something like this:
Code:
$this->getModelFromCache('EWRporta_Model_Modules')->getModules();
$this->getModelFromCache('EWRporta_Model_Modules')->getModulePermissions();
$this->getModelFromCache('EWRporta_Model_Modules')->parseModules();
I see in XenForo, very often:
Code:
$moduleModel = $this->getModelFromCache('EWRporta_Model_Modules');
$moduleModel->getModules();
$moduleModel->getModulePermissions();
$moduleModel->parseModules();
Does this methodology reduce load on subsequent entries? Or is it simply for aesthetics?
 

Shadab

Well-known member
#2
No. For all practical purposes, it doesn't make any difference. The model object, once created, is placed in the cache and on each subsequent access, the cached object is returned. Instead of instantiating the object every time.

But assigning the model object to a separate variable has its advantage too.
Mainly, code auto-completion in IDEs...
Code:
/* @var $moduleModel EWRporta_Model_Modules */
$moduleModel = $this->getModelFromCache('EWRporta_Model_Modules');

$moduleModel->g|
 

Despair

Active member
#4
I don't know XenForo code (or even Zend for that matter), but could you not chain it to be even shorter?
PHP:
$this->getModelFromCache('EWRporta_Model_Modules')
    ->getModules()
    ->getModulePermissions()
    ->parseModules();
Maybe not in this case?
 

Luke F

Well-known member
#5
I don't know XenForo code (or even Zend for that matter), but could you not chain it to be even shorter?
PHP:
$this->getModelFromCache('EWRporta_Model_Modules')
    ->getModules()
    ->getModulePermissions()
    ->parseModules();
Maybe not in this case?
The functions don't return an instance of the model, so no.