Advertisement
Guest User

ClassWithOptions Example

a guest
Nov 7th, 2011
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.76 KB | None | 0 0
  1. <?php
  2.  
  3.     /**
  4.      * The static members defined in this class, pertaining
  5.      * to the options, are required by several classes to
  6.      * manage default, static, and instance configuration
  7.      * options, each overriding the last respectively.
  8.      *
  9.      * The instance method is just an example.
  10.      */
  11.     class ClassWithOptions{
  12.    
  13.         protected static $_options = array();
  14.         protected static $_options_defaults = array(
  15.             'flag_foo' => true,
  16.             'flag_bar' => true,
  17.         );
  18.        
  19.         public static function setOption($name, $value){
  20.             static::$_options[$name] = $value;
  21.         }
  22.        
  23.         public static function setOptions(Array $options, $merge = true){
  24.             if($merge){
  25.                 static::$_options = array_replace(static::$_options, $options);
  26.             }else{
  27.                 static::$_options = $options;
  28.             }
  29.         }
  30.        
  31.         public static function getOption($name){
  32.             if(isset(static::$_options[$name])){
  33.                 return static::$_options[$name];
  34.             }
  35.             if(isset(static::$_options_defaults[$name])){
  36.                 return static::$_options_defaults[$name];
  37.             }
  38.             return null;
  39.         }
  40.        
  41.         public static function getOptions(){
  42.             return array_replace(static::$_options_defaults, static::$_options);
  43.         }
  44.        
  45.         public function useCaseOfClassWithOptions(Array $options = array()){
  46.             $options = array_replace(static::getOptions(), $options);
  47.             if(true === $options['flag_foo']){
  48.                 // do the foo
  49.             }
  50.             if(true === $options['flag_bar']){
  51.                 // do the bar
  52.             }
  53.         }
  54.    
  55.     }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement