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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kilbad
Forum Commoner
Posts: 28
Joined: Wed Apr 02, 2008 3:51 pm

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

Post 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');
 
*/
  
?> 
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

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

Post 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.
Last edited by RobertGonzalez on Fri Apr 18, 2008 11:24 am, edited 1 time in total.
Reason: Fixed a few small errors in my own post
Post Reply