Jump to content

Which sytax do you prefer?


trq

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.