Jump to content

Pretty keen to get some feedback on this prototype.


trq

Recommended Posts

Hi,

 

I havent used traits yet to implement anything..so pls excuse any inexperience with it...but I was thinking if the options object is necessary.

 

I mean can its use be simplified as

 

class Foo
{
    use Options;

    public function __construct(array $options = array())
    {
        $this->setDefaults([
            'foo' => 'foo' ,
            'bar' => ['required'=>true],
            'boo' => ['required'=>true,'type'=>'Something']
        ]);

        $this->setOptions($options);
    }
}

 

And in trait definition

 

 

 private function setOptions(array $options)
    {
        foreach ($options as $key => $value) {
            if (isset($this->options[$key])) {
              
           //validate if the value passed in $options for $key is the type specified in $this->options[$key]
                
            } else {
                $this->options[$key] = $value;
            }
        }

    }

 

Regards,

Sandeep

 

 

Link to comment
Share on other sites

Yeah, I have already toyed with a similar idea (see the botton of the blog post). The only problem is, it becomes difficult to tell if the array you have set for the 'bar' option defines default values 9as you posted) or is actually the default value for 'bar' itself.

 

I am going to stick with going down the Option object path, even though it does meen an extra dependency.

Link to comment
Share on other sites

The only problem is, it becomes difficult to tell if the array you have set for the 'bar' option defines default values 9as you posted) or is actually the default value for 'bar' itself.

 

Why not solve this by specifying the default value in the 'default' key . Ie

 

This defaults aray would become

 

$this->setDefaults([

            'foo' => ['default'=>'foo' ],

            'bar' => ['required'=>true],

            'boo' => ['required'=>true,'type'=>'Something']

        ])

 

Link to comment
Share on other sites

Yeah, I've been down that road a little too. I've been playing with these ideas for the last few days and am still convinced that I'll stick with the extra dependency on the Option object.

 

It just seems the cleanest and with the DI ideas I also have kicking around it will be easy enough to override Option if required.

Link to comment
Share on other sites

×
×
  • 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.