View Single Post
Old 01-24-2009, 05:15 PM   #2 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Smile

Try the following code. What it does is allows you to pass segments of the settings to the function _addToAndGetSettings. It only adds that segment to the main array once, and then only adds it when you really need it. It doesn't construct the entire array, with your 200 odd items.

Just one question though, why not use a database for something like this? It would be much easier, and maybe more efficient, too.

php Code:
class Products
{
    private static $m_pInstance;
    private $m_aSettings;
   
    /**
    * @return Products
    **/

    public function getInstance()
    {
        if (!isset(self::$m_pInstance))
        {
            self::$m_pInstance = new self();
        }
       
        return self::$m_pInstance;
    }
   
    private function _addToAndGetSettings($szFunction, $aItems)
    {
        if (!isset($this->m_aSettings[$szFunction]))
        {
            $this->m_aSettings[$szFunction] = $aItems;
        }
       
        return $this->m_aSettings[$szFunction];
    }
   
    public function getProductOne()
    {
        $aSettings = array
        (
            'name' => 'Item 1',
            'price' => 1.99
        );
       
        return (object) $this->_addToAndGetSettings(__FUNCTION__, $aSettings);
    }
   
    public function getProductTwo()
    {
        $aSettings = array
        (
            'name' => 'Item 2',
            'price' => 4.49
        );
       
        return (object) $this->_addToAndGetSettings(__FUNCTION__, $aSettings);
    }
}

echo Products::getInstance()->getProductOne()->name;
echo Products::getInstance()->getProductOne()->price;

echo Products::getInstance()->getProductTwo()->name;
echo Products::getInstance()->getProductTwo()->price;
Attached Files
File Type: php Products.php (1.1 KB, 152 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
MJ_ (01-24-2009)