kohana / ohanzee-helpers

Other
56 stars 10 forks source link

Array helper for properties (by trait) #7

Open Sett opened 10 years ago

Sett commented 10 years ago

Sometimes (more often, than wanted) there are array-properties in a class. It would be so cool, to get helpers for working with them.

I can show you the realization of this:

class My
{
    use Ohanzee\Helper\Trait\Arr;

    public $data = [
        'foo' => ['bar' => 'foobar']
    ];

    // somewhere in a methods code
    ...
    // Getting $this->data['foo']['bar']
    $this->{'foo.bar'}; 
    ...
}

What do you think about it?

shadowhand commented 10 years ago

I'm not so sure about the syntax $this->{'foo.bar'} but I generally like the concept. Would be really great if there was both WritableArr and ReadableArr, where ReadableArr would let you read data out, but not modify values.

Sett commented 10 years ago

Syntax will work wright, if you about it. Due to readbale/writable, it can be in such way:

//readable by using __get $this->$key; // or $this->{'some:deep:key'}

// writable by using __call $this->$key($value); // or $this->{'some:deep:key'}($value);

I have one more question: to the concept of the Ohanzee project, can I use already created classes, or for each "feature" should be a separated class?