Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * The static members defined in this class, pertaining
- * to the options, are required by several classes to
- * manage default, static, and instance configuration
- * options, each overriding the last respectively.
- *
- * The instance method is just an example.
- */
- class ClassWithOptions{
- protected static $_options = array();
- protected static $_options_defaults = array(
- 'flag_foo' => true,
- 'flag_bar' => true,
- );
- public static function setOption($name, $value){
- static::$_options[$name] = $value;
- }
- public static function setOptions(Array $options, $merge = true){
- if($merge){
- static::$_options = array_replace(static::$_options, $options);
- }else{
- static::$_options = $options;
- }
- }
- public static function getOption($name){
- if(isset(static::$_options[$name])){
- return static::$_options[$name];
- }
- if(isset(static::$_options_defaults[$name])){
- return static::$_options_defaults[$name];
- }
- return null;
- }
- public static function getOptions(){
- return array_replace(static::$_options_defaults, static::$_options);
- }
- public function useCaseOfClassWithOptions(Array $options = array()){
- $options = array_replace(static::getOptions(), $options);
- if(true === $options['flag_foo']){
- // do the foo
- }
- if(true === $options['flag_bar']){
- // do the bar
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement