MJ_ Posted January 24, 2009 Share Posted January 24, 2009 I have a configuration file in which a lot of settings are stored in arrays, something like: $productOne['NAME'] = array('example1','example2','example3'); $productOne['.. = .... $productTwo['NAME' = array('xxxxx1','xxxxx2'); $productTwo['.. = .... (about 200 declarations) I don't need all settings on every page, but I do need the settings as much that I prefer this option when compared to storing the settings in the database. I include this settings file on every page. I tried to find ways to improve the config file's efficiency. I thought maybe PHP objects would be a solution. I didn't have much knowledge in this area, and after reading some stuff about PHP/OOP I drew the conclusion that objects wouldn't make much of a difference, except the fact that the arrays could be declared ONLY when the settings were needed in the page (the array's wouldn't be declared automatically at each page load, but only when the instance of the class was created). I thought I could do the same by making functions for each array group, and call that function when I need to declare the array: this way, the arrays are also ONLY being declared when I need one on the page, and not automatically on every page load. In the end I don't really know what is the best option here. Then I found a module in PHP called 'memcache'. This at least sounded like a solution to improve the efficiency. Is it possible to store the config settings in the cache, so I won't need to reload and re-declare all array's every time a new page loads? Or does this option only increase memory usage (like compared to when I store all settings in $_SESSION vars)? Other solutions are also welcome. This is what I have made so far (a possible solution): // first type class ProductOne { private static $instance; private $product; public function getInstance(){ if(!isset(self::$instance)){ self::$instance = new self(); } return self::$instance; } public function getValue($key,$subKey){ switch($key){ case "name":{ if(!$this->product){ $this->product = array('name1','name2','name3','name4','name5'); } return $this->product[$subKey]; break; } case "size":{ if(!$this->product){ $this->product = array('size1','size2','size3','size4','size5'); } return $this->product[$subKey]; break; } case "color":{ if(!$this->product){ $this->product = array('color1','color2','color3','color4','color5'); } return $this->product[$subKey]; break; } } } } // another type class ProductTwo { private static $instance; private $product; public function getInstance(){ if(!isset(self::$instance)){ self::$instance = new self(); } return self::$instance; } function getValue($key,$subKey){ switch($key){ case "name":{ if(!$this->product){ $this->product = array('name1','name2','name3','name4','name5'); } return $this->product[$subKey]; break; } case "something":{ if(!$this->product){ $this->product = array('1','2','3','4','5'); } return $this->product[$subKey]; break; } } } } echo ProductOne::getInstance()->getValue("size",3); echo "<br>"; echo ProductTwo::getInstance()->getValue("something",2); In the end I will have about 10 different classes. I think I will put them in separate files then and use the __autoload to load the class settings when I need one. A short insight in the situation: There are main groups 'A','B','C','D','E' In every group there are about ten to twenty different classes (like the ProductOne and ProductTwo). I would manage these files in some directories and subdirectories. I will include each file (class) when I need the settings array (with __autoload). If you have other views on this please let me know. I also hope you will give comments on my code. Quote Link to comment https://forums.phpfreaks.com/topic/142279-config-file-arrays-oop-memcache/ Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 You could store your settings in a flatfile as some kind of delimitated string(s) or serialized or xml style, or store it in a db in columns, and retrieve as necessary. Quote Link to comment https://forums.phpfreaks.com/topic/142279-config-file-arrays-oop-memcache/#findComment-745381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.