01-24-2009, 05:15 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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;
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|