2011/12/4 Clint M Priest <[email protected]>:
> Updated patch w/o white-space:
http://www.clintpriest.com/patches/accessors_v1.patch
>
> In the end it is a relatively simple patch. The new syntax effectively
creates internal functions on the object and the system looks for those
functions and calls them at the appropriate time.
>
> Example:
> class z {
> public $Hours {
> public get { return $this->_Hours; }
> protected set { $this->_Hours = $value; }
> }
> }
>
> Defines:
> $o->__getHours();
> $o->__setHours($value);
You forgot to declare the backing field z::$_Hours in this example.
>From a semantic point of view, I find it misleading to first declare $Hours
as public, then lowering the bar by making the set-accessor protected.
The most common use-case for accessors is public - so I would suggest a
syntax more along the lines of this:
class z {
private $_hours;
get $hours { // accessor is public by default
return $this->_hours;
}
protected set $hours {
$this->_hours = $hours; // local variable $hours is the new value
}
}
And perhaps a short form for added convenience, where the backing-field is
automatically added for you - e.g.:
class z {
get $hours { // accessor is public by default
return $value; // $value provides access to the backing field (same way
$this provides access to the object context)
}
protected set $hours {
$value = $hours; // local variable $hours is the new value, $value
references the backing field
}
}
thoughts?