View Single Post
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