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 6140859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:10:26+00:00 2026-05-23T18:10:26+00:00

I need to have some configuration options on my website. I thought it would

  • 0

I need to have some configuration options on my website.

I thought it would be easiest to maintain if different options are placed in different files.

Also I need to have a class to retrieve the options from different configuration files.

In the directory structure of my website I created a directory called /setup

In this directory I have several files for the different configuration options, eg: /setup/base.php

The contents of base.php will look something like the following:

$setup = new stdClass();
$setup->currencies = array('USD', 'EUR', );
$setup->locations = array('local', 'international', );

I would like to create a class which reads the file and returns the different options.

class Options
{
    function __construct($option)
    {
        if (!is_file(SETUP_DIR.'/'.$option.'.php')) {
            thrown new Exception('Configuration file not found.');
        }

        $options = // get information from file

        return $options; // this should return the currencies and locations
    }
}

$options = new Options('base');

However I don’t know whether this is the correct way of doing it.

If so I cannot think of a way to retrieve the options from the setup files in the class.

Can you help me with this or at least point me in the right direction?

  • 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-23T18:10:26+00:00Added an answer on May 23, 2026 at 6:10 pm

    Well, I don’t think there is a right way for this one: Zend uses .ini files, Codeigniter has a set of arrays, and Symfony uses YAML. WordPress stores most everything in the database, and has one config file which it just includes.

    Personally, I’m partial to ini files — ini is something which is used all over the place, so it has a feeling of, “I can reuse this if necessary”, but I think that the only “wrong” solution here is one which is inconsistent — if you’re using ini, use ini, if arrays, arrays, but don’t mix.

    In your case, there are a couple of options. These two seem to be among the most common. (both of these examples assumes that the stdClass object is named $options in the loaded file) You could create a wrapper:

    class Options
    {
        private $_options;
        function __construct($option)
        {
            if (!is_file(SETUP_DIR.'/'.$option.'.php')) {
                thrown new Exception('Configuration file not found.');
            }
    
            require(SETUP_DIR.'/'.$option.'.php');
            $this->_options = $options;
            // you shouldn't put a return in a constructor 
        }
    
        // this will point to the internal _options variable.
        // making it a read-only access to the values from $option
        public function __get($name){ return $this->_options->$name; }
    }
    

    Or, you could use a Singleton pattern and just return the objects in the individual classes:

    class OptionsRetriever
    {
        private $_fetched;
        private static $_instance;
    
        private __construct(){}
    
        public static function &getInstance()
        {
            if( !isset( self::$_instance ) ) self::$_instance = new OptionsRetriever();
            return self::$_instance;
        }
    
        public function getConfig( $name )
        {
            if( !isset( $this->_fetched[ $name ] ) )
            {
                require(SETUP_DIR.'/'.$name.'.php');
                $this->_fetched[ $name ] = $options;
            }
            return $this->_fetched[ $name ];
        }
    }
    

    Or, you could combine them:

    class Options
    {
        private $_options;
        function __construct($options)
        {
            $this->_options = $options;
        }
    
        public function __get($name){ return $this->_options->$name; }
    }
    
        // replace getConfig with this
        public function getConfig( $name )
        {
            if( !isset( $this->_fetched[ $name ] ) )
            {
                require(SETUP_DIR.'/'.$name.'.php');
                $this->_fetched[ $name ] = new Options( $options );
            }
            return $this->_fetched[ $name ];
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Experts - I need some advice in the following scenario. I have a configuration
I have some data files that I need to install together with my application
In one of the configuration files for my project I need to append some
I need to have some information about the scoping in JavaScript. I know that
I need to have some object hanging around between two events I'm interested in:
I have some particular need to clone an element (image) and be draggable inside
I have some settings I need in a Javascript file -- servers to connect
I have some HTML that I need to toggle between two structures - one
I have some text i need to filter out a list of bad words
we have some C++ code that we need to create a make file in.

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.