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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:55:18+00:00 2026-05-17T01:55:18+00:00

For my project I wrote a small config class that loads its data from

  • 0

For my project I wrote a small config class that loads its data from a .ini file. It overwrites the magic __get() method in order to provide simplified access to the (read only) config values.

Example config.ini.php:

;<?php exit; ?>
[General]
auth = 1
user = "halfdan"

[Database]
host = "127.0.0.1"

My config class (singleton pattern – simplified here) looks like this:

class Config {
    protected $config = array();

    protected function __construct($file) {
        // Preserve sections
        $this->config = parse_ini_file($file, TRUE);
    }

    public function __get($name) {
        return $this->config[$name];
    }
}

Loading the config would create an array structure like this:

array(
  "General" => array(
    "auth" => 1,
    "user" => "halfdan"
  ),
  "Database" => array(
    "host" => "127.0.0.1"
  )
)

It’s possible to access the first level of the array by doing Config::getInstance()->General, and the values using Config::getInstance()->General['user']. What I really want though is being able to access all configuration variables by doing Config::getInstance()->General->user (syntactic sugar). The array is not an object and the “->” isn’t defined on it, so this simply fails.

I thought of a solution and would like to get some public opinion about it:

class Config {
  [..]
  public function __get($name) {
    if(is_array($this->config[$name])) {
      return new ConfigArray($this->config[$name]);
    } else {
      return $this->config[$name];
    }
  }
}

class ConfigArray {
  protected $config;

  public function __construct($array) {
    $this->config = $array;
  }

  public function __get($name) {
    if(is_array($this->config[$name])) {
      return new ConfigArray($this->config[$name]);
    } else {
      return $this->config[$name];
    }
  }
}

This would allow me to chain my configuration access. As I’m using PHP 5.3 it might also be a good idea to let ConfigArray extend ArrayObject (SPL is activated per default in 5.3).

Any suggestions, improvements, comments?

  • 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-17T01:55:19+00:00Added an answer on May 17, 2026 at 1:55 am

    If the elements of your $this->config array are also instances of your Config class, then it works.

    The Zend Framework has a similar component they call Zend_Config. You can download the source and examine how they implemented it. They didn’t have to go all the way to extending ArrayObject.

    The Zend_Registry class has similar usage and it does extend ArrayObject. The code for Zend_Registry is somewhat simpler as a result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I imported LuaInterface into a console project, referenced it, and wrote a small test
So I wrote a project-management program for a small business using Microsoft Access 2007.
I am working on a small project in C++ that requires me to create
I wrote a small servlet and jsp project on eclipse and tomcat 5.5 ,
I wrote a JSON-API in NodeJS for a small project, running behind an Apache
I wrote a small macrodef in separate file: macrodefs.xml <macrodef name=do-cool-stuff> <attribute name=message/> <sequential>
I have a project that I wrote in PHP/symfony that uses 45 tables. I'm
I wrote a small C++ program in VS2k8. When I launch it from windows
I wrote a small script that provided a URL (For example: code.google.com/ajax/123/1235/214) Would tell
I wrote a small application to use as a sandbox for testing ideas that

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.