Off the cuff, may have a bug or two, but you get the idea:

function get_config() {

  static $config;

  if (empty($config)) {
    // Something to load the configuration here.
  }

  $params = func_get_args();
  $return = $config;
  foreach ($params as $param) {
    if (isset($return[$param]) {
      $return = $return[$param];
    }
    else {
      trigger_error(...);
    }
  }
  return $return;
}

$bob = get_config('thing', 'silly', 'bob');

No, not as fast as global $config;, but more self-documenting and therefore 
easier to maintain and the difference is smaller than the cost of one SQL 
query.

Human cycles cost more than CPU cycles.  


On Saturday 17 November 2007, Sam Barrow wrote:
> Not a bad idea, however in my case (don't know about others) I have very
> deep arrays i use for my configuration. This would be more of a pain to
> use with these get and set functions. Also, the performance would
> probably be worse than just directly accessing the variable.
>
> Good point about not fixing what's not broken, but I think in this
> context it couldn't hurt to fix it. I have already fixed it, my patch is
> already written and I will continue testing it but I haven't come across
> any problems using it.
>
> On Sat, 2007-11-17 at 01:05 -0500, Carl P. Corliss wrote:
> > Sam Barrow wrote:
> > > Thanks everyone, I knew this, but I didn't want to use runkit because
> > > it is a beta, and i don't want all that other stuff, just superglobals.
> > > Also, runkit only allows you to use php.ini, but my patch allows you to
> > > specify superglobals in your script with the keyword "superglobal" by
> > > saying:
> > >
> > > superglobal $var1, $var2 ;
> >
> > I don't get why you can't just use a Registry pattern here. Having a
> > simpleRegistry object that you can throw data into and pull out of not
> > only allows you to keep your global space clean, but allows you to
> > encapsulate your "global" data and access it via a simple interface.
> > Sure, it might be a few extra keystrokes to type something akin to:
> > Registry::get('var1'); but, personally, I think the trade-off is well
> > worth it.
> >
> > simple example class:
> > --------------------
> > class Registry {
> >      protect function __construct() {} // no instantiation - static class
> >      static protected $data = array();
> >
> >      static public function get($name) {
> >          return (isset(self::$data[$name]) ? self::$data[$name] : null);
> >      }
> >
> >      static public function set($name, value) {
> >          self::$data[$name] = $value;
> >      }
> > }
> > --------------------
> >
> > summary: why "fix" what ain't really broke...?
> >
> > Cheers!,
> >
> > --
> > Carl


-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to