Created
February 2, 2011 15:23
-
-
Save webmozart/807833 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The current way, using addOption(), addRequiredOption() and getOption() | |
| + Concise | |
| + Error if option is not supported | |
| - Slow, because the addOption() calls are executed for every new instance | |
| - Base class Configurable needed | |
| - Overriding options in parent fields very intransparent | |
| - Options need special treatment ($this->required vs. $this->getOption('required')) | |
| - Problems with accessing required options before parent::__construct() | |
| <?php | |
| class Thing extends Configurable | |
| { | |
| public function configure() | |
| { | |
| $this->addOption('something'); | |
| // now we can do $this->getOption('something') | |
| } | |
| } | |
| // WHAT'S REALLY UGLY #1: Overriding parent options | |
| class SubThing extends Thing | |
| { | |
| protected function configure() | |
| { | |
| // overrides parent value because called before parent::configure() | |
| $this->addOption('something', 'default'); | |
| parent::configure(); | |
| // does not override parent value because already added | |
| $this->addOption('something', 'default'); | |
| } | |
| } | |
| // WHAT'S REALLY UGLY #2: Accessing options before parent::__construct() | |
| class OtherThing extends Thing | |
| { | |
| // this could even be in the parent class so that you don't even know | |
| // that doSomethingBeforeParentConstruct() is called before the root constructor | |
| public function __construct(array $options = array()) | |
| { | |
| $this->doSomethingBeforeParentConstruct(); | |
| parent::__construct($options); | |
| } | |
| protected function configure() | |
| { | |
| $this->addRequiredOption('newoption'); | |
| } | |
| public function doSomethingBeforeParentConstruct() | |
| { | |
| // newoption is an required option, so we assume it is there | |
| $this->getOption('newoption')->doSomething(); // BOOM! option is NULL | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment