shahinbdboy / google-api-php-client

Automatically exported from code.google.com/p/google-api-php-client
Apache License 2.0
0 stars 0 forks source link

configuration is too brittle #141

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
using the latest 0.5.0 release

Having configuration as a global variable is unwise and too easily screwed up.  
Doing it as an associative array is also very brittle and vulnerable to typos.

Why not use a static class instead?

<?php
namespace GData;
class Options
{
  public $use_objects = false;
  // ... etc
  static $o = null;
  static function initialize()
  {
    $o = new self;
  }

  function __get($var)
  {
    throw new Exception('Unknown configuration option ' . $var);
  }

  function __set($var, $value)
  {
    throw new Exception('Unknown configuration option ' . $var);
  }
}
Options::initialize();
// to access a config item, we do:
// $objects = Options::$o->use_objects;
?>

This would also allow easy replacement without having to do some weird file 
manipulation.

<?php
class MyConfig extends GData\Options
{
  public $use_objects = true;
}
Options::$o = new MyConfig;

You can still support loading of file to change options, with this kind of code:

<?php
namespace GData;
include $otherconfig;
foreach ($apiConfig as $key => $value) {
  Options::$o->$key = $value;
}
?>

This would allow really easy tracking down of bugs because of a misspelled 
option value, something that is impossible now because it simply fails silently 
without changing the config value

Original issue reported on code.google.com by g...@chiaraquartet.net on 18 Jun 2012 at 9:07

GoogleCodeExporter commented 8 years ago
Thanks for the suggestion, we will be looking at configuration options.

Original comment by ianbar...@google.com on 22 Mar 2013 at 2:17