staticOptions - setting defaults for various classes...
| From: | Alan Knowles | Date: | Thu, 20 Jun 2002 05:16:55 +0000 |
| Subject: | staticOptions - setting defaults for various classes... | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-7223@lists.php.net to get a copy of this message | ||
Ive been a busy bee, building a whole app, attempting to use as many pear classes as possible.. one of the things that appears to be missing is a way to set the default options on various classes
an example would be:
Mail: default factory method/host etc.
HTML_Quickforms_date: date format and range..
I'm sure there are a few more classes that take options of some sort for class initiation that could do with a 'site-wide' mechanism of pre-setting the defaults.., the mechanism below is what I have ended up using for DB_DataObject,
at the application start point..
require_once('DB/DataObject.php');
require_once('Mail.php');
require_once('HTML_QuickForms_date');
$config = parse_ini_file('myconfig.ini');
DB_DataObject::staticOptions($config['DB_DataObject']);
Mail::staticOptions($config['Mail']);
HTML_QuickForms_date::staticOptions($config['HTML_QuickForms_date']);
obviously this could be done using the Config class as well, using $config->getValues('/DB/DataObject'); as well.. or any other method that the user prefers....
the staticOptions method looks like this..
/**
* Get/Set the Options using array or object
*
* to fetch: use $options = &DB_DataObject::staticOptions();
* to set DB_DataObject::staticOptions();
* or DB_DataObject::staticOptions(array('database'=>'mysql.....'));
* @param object Config (Optional) or string ('realm')
* @param array Options
* @access public * @return array Options*/ function &staticOptions() {
static $options = array();
if ($args = &func_get_args()) {
$options = $args[0];
}
return $options;
}
for something like HTML_QuickForms_date the change would look something like this..
Function HTML_QuickForm_date ($elementName, $value=null, $new_options=array())
{
$options = HTML_QuickForm_date::staticOptions(); // copy not reference
foreach ($new_options as $k=>$v) {
$options[$k] = $v;
}
HTML_QuickForm_element::HTML_QuickForm_element($options);
.........
Anyway - anybody got any thoughts on this??
regards
alan