trq Posted December 27, 2011 Share Posted December 27, 2011 I am currently prototyping some code that will enable named arguments to functions via an array. I have ended up with two different solutions: public function trigger(array $options) { $ops = $this->setOptions([ 'name' => ['required' => true, 'unless' => ['event']], 'params' => ['required' => true, 'unless' => ['event'], 'type' => 'array'], 'callback' => ['type' => 'callable'], 'context' => ['type' => 'object'], 'event' => ['class' => 'Event'] ], $options); } public function trigger(array $options) { $ops = $this->setOptions([ 'name' => (new Option())->required()->unless('event'), 'params' => (new Option())->required()->unless('event')->type('array'), 'callback' => (new Option())->type('callable'), 'context' => (new Option())->type('object'), 'event' => (new Option())->class('Event') ], $options); } Now, I am in the predicament of deciding which syntax I prefer. The first uses native arrays but the underlying code is pretty messy and has the potential of being harder to extend. The second is allot cleaner to implement but does introduce an extra dependency in the Option object. This does however have the benefit of making it easier to extend. Has anyone got an opinion on which syntax they would prefer from a users perspective? In both cases the setOptions() method is provided via a trait. Quote Link to comment https://forums.phpfreaks.com/topic/253915-which-sytax-do-you-prefer/ Share on other sites More sharing options...
ignace Posted December 28, 2011 Share Posted December 28, 2011 How about: $this->configure() ->option('name')->required()->unless('event') ->option('params')->required()->unless('event')->type('array') ... The Option dependency is than hidden and allows for some abstraction. interface Option { public function option($name); } Quote Link to comment https://forums.phpfreaks.com/topic/253915-which-sytax-do-you-prefer/#findComment-1301794 Share on other sites More sharing options...
trq Posted December 28, 2011 Author Share Posted December 28, 2011 That is a much nicer looking implementation. Unfortunately, I'm not sure I can get it working. I would need $this->configure() to return the options ready for use. I will have a dig around though and see what I can get working. There is an initial prototype available here if you'd like to have a look. Quote Link to comment https://forums.phpfreaks.com/topic/253915-which-sytax-do-you-prefer/#findComment-1301990 Share on other sites More sharing options...
trq Posted December 30, 2011 Author Share Posted December 30, 2011 I ended up going with the second one and have just finished writing the documentation now. https://github.com/proem/proem/wiki/The-Opt-Component Quote Link to comment https://forums.phpfreaks.com/topic/253915-which-sytax-do-you-prefer/#findComment-1302454 Share on other sites More sharing options...
sandeep529 Posted December 30, 2011 Share Posted December 30, 2011 Hi.. I would ask myself the question... which is done often? Extending the options object or using it? I would choose the method that will make the most often used operation simple.... Quote Link to comment https://forums.phpfreaks.com/topic/253915-which-sytax-do-you-prefer/#findComment-1302471 Share on other sites More sharing options...
trq Posted December 30, 2011 Author Share Posted December 30, 2011 You can already add quite a bit of functionality to the Option object on the fly without needing to extend it. Though I do also plan on having a better look at how I can make extending classes within the framework allot simpler. I just haven't gotten there as yet. Quote Link to comment https://forums.phpfreaks.com/topic/253915-which-sytax-do-you-prefer/#findComment-1302496 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.