Optimize Class for Cache

LPH

Well-known member
In my project, there are many files (located externally from XenForo) that use the following type of code:

PHP:
/** @var  $threadModel XenForo_Model_Thread */
$forumModel = XenForo_Model::create( 'XenForo_Model_Forum' )->getModelFromCache( 'XenForo_Model_Forum' );

I'd like to optimize the project by grouping common methods, repetitive code is eliminated, and the call to `getModelFromCache` is done properly. There is a XenWord_Forums class that I'm building but my understanding of OOP is very week. Therefore, I'm looking for help on the best way to organize and optimize the project.

Using this simple skeleton class, as an example, would it be best to take $forumModel and declare it at the top?

Code:
$forumModel = XenForo_Model::create( 'XenForo_Model_Forum' );

PHP:
<?php
class XenWord_Forums extends XenWord {

    public function __construct() {

    }

    /**
    * @param       $id
    * @param array $fetchOptions
    *
    * @return mixed
    * @throws XenForo_Exception
    */
    public function getForumById( $id, $fetchOptions = array() ) {
        /** @var $forumModel XenForo_Model_Forum getForumById */
        $forumModel = XenForo_Model::create( 'XenForo_Model_Forum' );
        return $forumModel->getForumById( $id, $fetchOptions );
    }

    /**
    * @param       $ids
    * @param array $fetchOptions
    *
    * @return array
    * @throws XenForo_Exception
    */
    public function getForumsByIds( $ids, $fetchOptions = array() ) {
        /** @var $forumModel XenForo_Model_Forum getForumByIds */
        $forumModel = XenForo_Model::create( 'XenForo_Model_Forum' );
        return $forumModel->getForumsByIds( $ids, $fetchOptions );
    }

    /**
    * @param array $conditions
    * @param array $fetchOptions
    *
    * @return array
    * @throws XenForo_Exception
    */
    public function getForums(array $conditions = array(), array $fetchOptions = array()) {
        /** @var $forumModel XenForo_Model_Forum getForumByIds */
        $forumModel = XenForo_Model::create( 'XenForo_Model_Forum' );
        return $forumModel->getForums($conditions, $fetchOptions);
    }

}

new XenWord_Forums();

Thank you for any explanation and providing resources so that I may learn more about OOP.
 
You could do something like;

PHP:
class MyClass
{
protected $_model = null;
public function test()
{
   $model = $this->_getModel();
}
protected function _getModel()
{
   if ($this->_model === null)
   {
     $this->_model = XenForo_Model::create('Model');
   }
   return $this->_model;
}
}
 
By the way you don't need to create the model and then use getModelFromCache on it. It was in your first code snip but not in the subsequent code so I can guess you removed it but yeah.
 
You could do something like;

PHP:
class MyClass
{
protected $_model = null;
public function test()
{
   $model = $this->_getModel();
}
protected function _getModel()
{
   if ($this->_model === null)
   {
     $this->_model = XenForo_Model::create('Model');
   }
   return $this->_model;
}
}
@batpool52!'s methods can store multiple models in the cache (well, he stole it from XF :p), that can store one. Either works really, depends on how many you need to store. If just one, you can do either. Or the first reply for multiple.
 
Top Bottom