SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | ||
3 | - | /** |
3 | + | /** |
4 | - | * The static members defined in this class, pertaining |
4 | + | * The static members defined in this class, pertaining |
5 | - | * to the options, are required by several classes to |
5 | + | * to the options, are required by several classes to |
6 | - | * manage default, static, and instance configuration |
6 | + | * manage default, static, and instance configuration |
7 | - | * options, each overriding the last respectively. |
7 | + | * options, each overriding the last respectively. |
8 | - | * |
8 | + | * |
9 | - | * The instance method is just an example. |
9 | + | * The instance method is just an example. |
10 | - | */ |
10 | + | */ |
11 | - | class ClassWithOptions{ |
11 | + | class Options{ |
12 | - | |
12 | + | |
13 | - | protected static $_options = array(); |
13 | + | private $_options = array(); |
14 | - | protected static $_options_defaults = array( |
14 | + | private $_options_defaults = array( |
15 | - | 'flag_foo' => true, |
15 | + | 'flag_foo' => true, |
16 | - | 'flag_bar' => true, |
16 | + | 'flag_bar' => true, |
17 | - | ); |
17 | + | ); |
18 | - | |
18 | + | |
19 | - | public static function setOption($name, $value){ |
19 | + | public function setOption($name, $value){ |
20 | - | static::$_options[$name] = $value; |
20 | + | static::$_options[$name] = $value; |
21 | - | } |
21 | + | } |
22 | - | |
22 | + | |
23 | - | public static function setOptions(Array $options, $merge = true){ |
23 | + | public function setOptions(Array $options, $merge = true){ |
24 | - | if($merge){ |
24 | + | if($merge){ |
25 | - | static::$_options = array_replace(static::$_options, $options); |
25 | + | static::$_options = array_replace(static::$_options, $options); |
26 | - | }else{ |
26 | + | }else{ |
27 | - | static::$_options = $options; |
27 | + | static::$_options = $options; |
28 | - | } |
28 | + | } |
29 | - | } |
29 | + | } |
30 | - | |
30 | + | |
31 | - | public static function getOption($name){ |
31 | + | public function getOption($name){ |
32 | - | if(isset(static::$_options[$name])){ |
32 | + | if(isset(static::$_options[$name])){ |
33 | - | return static::$_options[$name]; |
33 | + | return static::$_options[$name]; |
34 | - | } |
34 | + | } |
35 | - | if(isset(static::$_options_defaults[$name])){ |
35 | + | if(isset(static::$_options_defaults[$name])){ |
36 | - | return static::$_options_defaults[$name]; |
36 | + | return static::$_options_defaults[$name]; |
37 | - | } |
37 | + | } |
38 | - | return null; |
38 | + | return null; |
39 | - | } |
39 | + | } |
40 | - | |
40 | + | |
41 | - | public static function getOptions(){ |
41 | + | public function getOptions(){ |
42 | - | return array_replace(static::$_options_defaults, static::$_options); |
42 | + | return array_replace(static::$_options_defaults, static::$_options); |
43 | - | } |
43 | + | } |
44 | - | |
44 | + | |
45 | - | public function useCaseOfClassWithOptions(Array $options = array()){ |
45 | + | public function useCaseOfClassWithOptions(Array $options = array()){ |
46 | - | $options = array_replace(static::getOptions(), $options); |
46 | + | $options = array_replace(static::getOptions(), $options); |
47 | - | if(true === $options['flag_foo']){ |
47 | + | if(true === $options['flag_foo']){ |
48 | - | // do the foo |
48 | + | // do the foo |
49 | - | } |
49 | + | } |
50 | - | if(true === $options['flag_bar']){ |
50 | + | if(true === $options['flag_bar']){ |
51 | - | // do the bar |
51 | + | // do the bar |
52 | - | } |
52 | + | } |
53 | - | } |
53 | + | } |
54 | - | |
54 | + | } |
55 | - | } |
55 | + | |
56 | class ClassThatNeedsOptions { | |
57 | ||
58 | private $options; | |
59 | ||
60 | public function __construct(Options $options) { | |
61 | $this->options = $options; | |
62 | } | |
63 | ||
64 | public function useCaseOfClassWithOptions(Array $options){ | |
65 | $options = array_replace($this->options->getOptions(), $options); | |
66 | if(true === $options['flag_foo']){ | |
67 | // do the foo | |
68 | } | |
69 | if(true === $options['flag_bar']){ | |
70 | // do the bar | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | new ClassThatNeedsOptions(new Options); | |
76 |