Page 1 of 1

Setting up a default array(?) in settings file...

Posted: Fri Apr 18, 2008 11:15 am
by kilbad
Currently, I have a Registry class where I store default and/or general variables that are used in several classes (for example, a variable containing the root path address). I have included the class below which simply contains a (1) set function and (2) get function. However, I would like to add a (3) third function to import preset default values from another settings file, such that I can do a $registry->import('/app/settings.ini') type thing.

Could someone help me do this? Regardless, thank you all for your help on these forums!

Code: Select all

 
<?php
class Registry {
 
    private $store = array();
 
    public function __set($label, $object) {
        if(!isset($this->store[$label]))
        {
            $this->store[$label] = $object;
        }
    }
 
    public function __get($label) {
        if(isset($this->store[$label]))
        {
            return $this->store[$label];
        }
        return false;
    }
}
 
//Current example of usage
$registry = new Registry();
$registry->__set("Database Name", "mysql.internal");
echo $registry->__get("Database Name"); //Outputs "mysql.internal"
 
/* What I would like to add is some type of function that adds labels and objects from a settings file, so
    something like::
 
$registry->import('/app/settings.ini');
 
*/
  
?> 
 

Re: Setting up a default array(?) in settings file...

Posted: Fri Apr 18, 2008 11:23 am
by RobertGonzalez
I am not sure what you are setting up is going to work the way you expect it to. Generally a registry object is made as a singleton to ensure a single instance of the object so that settings values, getting values and checking values are all done on the same instance of the registry object.

In your code you can instantiate that object time and time again and have multiple versions of the object that do not share any of their settings after instantiation.

Also, you are using magic methods ( __set() and __get() ) so you should not be calling those as methods from the object but rather use their functionality.

In terms of importing a file, you can look at file_exists(), include and parse_ini_file() to get a little further along the path you are on.