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!

<?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');

*/

?> 

    Your import method could use [man]parse_ini_file[/man] and use your setter method on each array.

      Thank you for your reply. I looked at the php manual, and think this could definitely be an option.

      Would you mind providing some example code of how you would implement that function in an import()?

      Thanks in advance!

        registry.php

        <?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;
        }
        
        public function import($filename){
        if(is_file($filename)){
          $parsed = parse_ini_file($filename, true);
          foreach($parsed as $key => $value)
            $this->$key = $value;
        } else {
          throw new Exception("$filename does not exist!"); 
        } 
          }
        
        }
        $reg = new Registry();
        $reg->import('config.ini');
        echo '<pre>';
        print_r($reg);
        echo '</pre>';
        
        echo $reg->first['something'];
        ?>

        config.ini

        [first]
        something = "This is a test"
        something2 = "another Test"
        [second]
        haha = "I win!"
          Write a Reply...