Re: staticOptions - setting defaults for various classes...

From: Date: Fri, 21 Jun 2002 00:47:48 +0000
Subject: Re: staticOptions - setting defaults for various classes...
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-7255@lists.php.net to get a copy of this message
Ok,just spotted PEAR::getStaticProperty($class,$key); prehaps a 'complimentary method would be' function setStaticProperty($class,$key,$value) { $oldvalue = &PEAR::getStaticProperty($class,$key); $oldvalue = $value; } this solves all the issues below - if all the classes can use it to query for their default options???? $config = parse_ini_file('myconfig.ini',TRUE); foreach($config as $class=>$options) {
    PEAR::setStaticProperty($class,'options',$options);
} Any +'s on adding??? regards alan Alan Knowles wrote:
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


« previous php.pear.dev (#7255) next »