Does assigning Model variables reduce load?

Jaxel

Well-known member
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?
 
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|
 
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?
 
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.
 
Top Bottom