TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-24-2009, 03:55 PM   #1 (permalink)
MJ_
The Visitor
 
Join Date: Jan 2009
Posts: 2
Thanks: 1
MJ_ is on a distinguished road
Default 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
MJ_ is offline  
Reply With Quote
Old 01-24-2009, 05:15 PM   #2 (permalink)
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, 153 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)
Old 01-24-2009, 06:53 PM   #3 (permalink)
MJ_
The Visitor
 
Join Date: Jan 2009
Posts: 2
Thanks: 1
MJ_ is on a distinguished road
Confused

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:
// 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.

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.
MJ_ is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy to Modify Login Script with Hierarchical User Permissions and XML Account File Wildhoney Script Giveaway 4 05-04-2011 06:11 AM
Where is my file? superthin General 3 07-25-2009 09:48 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
Aptana Jaxer and file uploads xenon Advanced PHP Programming 2 06-06-2008 10:22 AM
Writing to XML file buildakicker General 8 02-06-2008 08:17 PM


All times are GMT. The time now is 04:34 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design