![]() |
config file arrays oop memcache
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['.. = .... (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. Thanks in advance ^^ |
1 Attachment(s)
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:
|
Thanks for your fast reply!
The example you give, is something I already tried to accomplish. Your code however contains some things I didn't think of yet and which may be useful. I think the downside of what you suggest is that it doesn't fully serve it's function with two-dimensional arrays (which I'm using). As I mentioned in the first post: $productOne['NAME'] = array('example1','example2','example3'); $productTwo['NAME'] = array('example4','example5','example6'); , but your code is more an improvement of: $productOne['NAME'] = 'item1'; $productTwo['NAME'] = 'item2'; (not mentioned the 'PRICE' and other keys) I could ofcourse replace 'name' => 'Item 1' in your code with 'name' => array('example1','example2','example3') , but then I would also do the same things at the 'price' and other keys. Then it still loads a lot of arrays when, for example, you only want the third key name of product one (it then also loads the price array and others). I have combined your code with my personal code which does separately load the arrays "name", "..." etc I hope you can take a look at it and tell me if this is right or if your solution would be better if two-dimensional arrays were supported. php Code:
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. On your question of why I don't use a database for this: If I use the database, I will have to create a database for every group (explained later). The tables will then be ProductOne, SomeOtherProduct etc, and the fields in one table will be, for example, 'name' , 'price' etc. But as I'm using two-dimensional arrays, my only option (I think) will be to separate the data with comma's in a field and then when I read the field explode the comma's. 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 rather 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. |
| All times are GMT. The time now is 01:59 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0