Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:12:24+00:00 2026-05-29T16:12:24+00:00

I am after some input/guidance.. I have an application that uses a central configuration

  • 0

I am after some input/guidance.. I have an application that uses a central configuration registry as show below..

The application parses a config directory for ini files and sets the arrays into the class using the name of the file as the index, as well as single configuration variables when required..

I can see a few problems arising including:

  1. Name clashes between files and vars set within the script
  2. The inability to pull nested array variables resulting in the following code:

    $databases = config::get(‘database’);
    $actual_record = $databases[‘default’];

I was tempted to add a 2nd get parameter for the nested value, however what happens in the future if i need to pull a 3rd or 4th level value.

class config
{
   private static $registry;

   /**
   *
   */
   private function __construct() {}

   /**
   *
   */
   public static function get($key)
   {
      if (isset(self::$registry[$key])) return self::$registry[$key];
      else return FALSE;
   }

   /**
   *
   */
   public static function set($key, $value, $overwrite = FALSE)
   {
      // Does the variable already exist?
      if (isset(self::$registry[$key]) && $overwrite === FALSE) 
         throw new Exception();

      self::$registry[$key] = $value;
   }
}

Thanks in advance for the help..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:12:25+00:00Added an answer on May 29, 2026 at 4:12 pm

    As proposed in the comments here is the code to make the dot separated names working. There might be a more efficient solution, I just threw this together for you.

    class Config
    {
        private
            $registry
        ;
    
        public function __construct($registry)
        {
            $this->registry = $registry;
        }
    
       public function get($identifier)
       {
            return $this->resolve(explode('.', $identifier), $this->registry);
        }
    
        private function resolve($entries, $in)
        {
            if(key_exists($entries[0], $in) && count($entries) > 1)
            {
                // We have more than one level to resolve
                $newIn = $in[$entries[0]];
                unset($entries[0]);
                return $this->resolve(array_values($entries), $newIn);
            }
            elseif(key_exists($entries[0], $in) && count($entries) == 1)
            {
                // We are at the bottom, let's return.
                return $in[$entries[0]];
            }
            // If we get here something went wrong.
            throw new Exception('Entry could not be resolved.');
        }
    }
    
    $cfg = new Config(
        array(
            'plain' => 'plain entry',
            'nested'    =>  array(
                'first' =>  'nested, first entry',
                'second'    =>  array(
                    'third' =>  'deeper nested entry'
                )
            )
        )
    );
    
    print_r($cfg->get('plain'))."\n";
    print_r($cfg->get('nested.first'))."\n";
    print_r($cfg->get('nested.second.third'))."\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application witch runs about 30 min. with some input data. It
After some stupid musings about Klingon languages, that came from this post I began
After some years of experience in the field, it seems to me that the
After some use Visual Studio 2008 when opening a solution that is checked into
I have created some JQuery that on click of specified HTML it then becomes
In my latest program, there is a button that displays some input popup boxes
After input some variable, PHP code: $CartItem = array( 'itemID' => $itemID, 'ProductName' =>
I have a form that hides a text input field dynamically, depending on the
I have some checkboxes that act as search filters. These search filters trigger an
How would you prompt the user for some input but timing out after N

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.